Remove only one document in MongoDB

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

db.collection.remove now has a justOne flag

http://docs.mongodb.org/manual/reference/method/db.collection.remove/#db.collection.remove


If there are multiple records and you want to delete only first record, then set justOne parameter in remove() method.

Here, you want to delete only 1. Thus, set "justOne" parameter as 1.

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

Tags:

Mongodb