Wednesday, January 29, 2014

How to delete document (Row) in MongoDB Collection (table)?

In this post we will explore how we can delete or remove document (row) from Collection (table) in MongoDB.

To delete document (row) MongoDB has provided remove() method. The remove() method is use to delete rows. You can delete one record or set of records of the entire data from collection(table). The syntax for remove() method is following:

db.collectioname.remove([deleteion_criteria],[justone])

To remove all the document (row) from MongoDB collection (table) we can issue the following command

db.collectionname.remove()

For example purpose let us delete all the record form deptdetails Collection in MongoDB.

To remove or delete records which match a particular condition, we can pass the deletion criteria. For example from the deptdetails table we can remove the document with the value "I am new data here".

Justone

The justone parameter with remove() method is use to tell MongoDB to remove one record or all the records. It has two values. 1 and 0. Setting value 1 will remove only one record.

For example we have two records with DeptCode 20 in deptdetails collection. We have run the remove() command with justone parameter value as 1 and it deleted one record only, as you can see in the below screen.

What is MongoDB?
How to get Started with MongoDB
How to create or drop Database in MongoDB?
How to create Collection (Table) in MongoDB?
... More

Popular Posts

Real Time Web Analytics