MongoDB Remove Delete Document Record From Collection | Java @ Desk
Home » » MongoDB Remove Delete Document Record From Collection

MongoDB Remove Delete Document Record From Collection

MongoDB Remove Delete Document Record From Collection

To remove/delete a record/document from MongoDB following command is used

db.collection.remove()


The remove function accepts 2 arguments
1) query document
2) justOne

Different ways to delete a document from collection are :
1) Remove All Documents from a Collection - In this case empty query parameter passed in the remove method are deleting all the documents from a collection. This is not equivalent to the drop() method which removes the collection as well as indexes associated with the collection

2) Remove All Documents that satisfy a given Condition - Query parameter for which the document need to be removed is passed in the remove method

3) Remove a Single Document that satisfy a given Condition - This is similar to the second option with an additional parameter of justOne parameter set to true or 1.

Example :
1) Remove All Documents from a Collection
db.loans.remove( { } )


2) Remove All Documents that satisfy a given Condition
db.loans.remove({'name':'Personal Loan'})
db.loans.remove({'name':'Home Loan'})


3) Remove a Single Document that satisfy a given Condition
db.loans.remove({'name':'Personal Loan'}, true )





0 comments:

Post a Comment