andrei
andrei

Reputation: 8582

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 function 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 ?

Upvotes: 1

Views: 3365

Answers (2)

Aurélien B
Aurélien B

Reputation: 4640

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)

Upvotes: 1

Karoly Horvath
Karoly Horvath

Reputation: 96258

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.

Upvotes: 0

Related Questions