Reputation: 32776
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))
But nothing happens. I did a print_r
and it says:
Array ( [err] => [n] => 0 [ok] => 1 )
Upvotes: 1
Views: 3564
Reputation: 13
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);
?>
Upvotes: 1
Reputation: 92
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));
Upvotes: 2
Reputation: 23354
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" } }
Upvotes: 0
Reputation: 32776
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));
Upvotes: 0