Remove only one document in MongoDB

后端 未结 9 2005
日久生厌
日久生厌 2021-01-12 23:42

when I call

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

This one line will delete all the matching condition documents.

9条回答
  •  别那么骄傲
    2021-01-12 23:50

    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});
      

提交回复
热议问题