Sunday, 21 January 2018

Delete Documents in MongoDB Collection

Delete Methods

MongoDB provides the following methods to delete documents of a collection:
db.collection.remove({})Delete a single document or all documents that match a specified filter.
db.collection.deleteOne({})Delete at most a single document that match a specified filter even though multiple documents may match the specified filter.
db.collection.deleteMany({})Delete all documents that match a specified filter.
Eg:-  db.users.deleteMany({ status : "A" })

      db.users.deleteOne( { status: "D" } )

      db.getCollection('Users').find({$and:[{"cid":2}, {"created-at":{ "$gte" : new ISODate("2018-01-22")}} ]})