Remove only one document in MongoDB

后端 未结 9 2029
日久生厌
日久生厌 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-13 00:13

    According to MongoDB db.collection.remove() reference:

    justOne

    An optional boolean value. To limit the deletion to just one document, set to true. Omit to use the default value of false and delete all documents matching the deletion criteria.

    You can set the justOne to true, in order to do it.

    Before MongoDB version 2.6:

    db.collection.remove(
       ,
       
    )
    

    MongoDB version 2.6 or later:

    db.collection.remove(
       ,
       {
         justOne: ,
         writeConcern: 
       }
    )
    

    Like this: db.collection.remove({"condition": "some condition"}, {justOne: true});.

提交回复
热议问题