MongoDb delete query
I'm trying to delete something from my mongoDb database based on the _id and i swear anything else gets deleted except what I want. I'm using a codeigniter library this is the function:
public funct开发者_如何学JAVAion delete($collection = "")
{
if(empty($collection))
{
show_error("No Mongo collection selected to delete from", 500);
}
try
{
$this->db->{$collection}->remove($this->wheres, array('fsync' => TRUE, 'justOne' => TRUE));
return(TRUE);
}
catch(MongoCursorException $e)
{
show_error("Delete of data into MongoDB failed: {$e->getMessage()}", 500);
}
}
And I'm doing this to delete
$this->mongo_db->delete('videos', $data = array('_id' => $id));
It definitely deletes but not what I want. I'm new to mongo so where should I check for errors ?
It can be caused by many elements.
- check if your $id is a string or a MongoId (if you use php official mongo driver, I assume its php code, just use
new MongoId($id_as_string)
; (It should be an MongoId) - you can also test with
safe
option (without sync) and check what you obtain as result ("safe" query return array not a boolean)
That function expects one argument, which is the name of the collection, you call it with two. Check the docs/source to see how you can specify the where document.
精彩评论