Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove only one document in MongoDB

Tags:

mongodb

when I call

db.collection.remove({'condition':'some condition'});

This one line will delete all the matching condition documents.

What If I want to remove only one of any or n-th matching condition?

like image 367
jwchang Avatar asked Oct 25 '11 01:10

jwchang


1 Answers

You need to perform two separate queries for this

  • take only one item from the matching filters

    var item = db.collection.findOne({'condition':'some condition'})
    
  • and delete the item by using the id

     db.collection.remove({_id: item._id});
    
like image 162
RameshVel Avatar answered Oct 12 '22 00:10

RameshVel