开发者

MongoDB + PHP Item wont remove()?

I'm not sure what I'm doing wrong, but with MongoDB and PHP i'm trying to do this:

$db->textures->remove(array("_id"=>$_GET['texture_id']),array("safe" => true))

B开发者_Python百科ut nothing happens. I did a print_r and it says:

Array ( [err] => [n] => 0 [ok] => 1 )


The following Oscar Godson's answer is correct but now deprecated since 1.5.0 :

$db->textures->remove(array("_id"=>new MongoId($_GET['texture_id'])),array("safe" => true));

should now be written using the "w" option like that :

$db->textures->remove(array("_id"=>new MongoId($_GET['texture_id'])),array("w" => 1));

Sources


To remove a document based on its ID, you need to ensure that you pass the ID as a MongoID object rather than just a string:

<?php 
$id = '4b3f272c8ead0eb19d000000';
// will not work:
$collection->remove(array('_id' => $id), true);

// will work:
$collection->remove(array('_id' => new MongoId($id)), true);
?>


Crap, looks like since im removing a MongoID i have to do this:

$db->textures->remove(array("_id"=>new MongoId($_GET['texture_id'])),array("safe" => true));


If an ID is not supplied when the record is saved then yes, you'll need to use the MongoID object to build the correct search criteria. You can, however, define the _id to be whatever you want - a plain integer, text, timestamp, etc - that you can use to search on as with any other property.

See the following CLI output as an example - the first object has an _id that contains an ObjectId type, but the second contains a simple string. A search by the string works as normal:

> db.test.save({ name: "Test Object 1"});
> db.test.save({ _id: "abc123", "name" : "Test Object 2" });
> db.test.find();
{ "_id" : ObjectId("4cca41c9d86d000000006d33"), "name" : "Test Object 1" }
{ "_id" : "abc123", "name" : "Test Object 2">
db.test.find({"_id" : "abc123"});
{ "_id" : "abc123", "name" : "Test Object 2" } }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜