Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

Delete Document in Mongodb: A Practical Guide

Understand Delete Document in Mongodb with clear explanations, practical examples, and useful tips. This updated guide covers the essential concepts and...

Table of Contents

Delete Document in Mongodb is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.

Remove () method in MongoDB

The remove () method in MongoDB helps delete the Document from Collection. The remove () method takes two parameters. The first parameter of the deletion criteria determines the Document to delete, and the second parameter is justOne.

  1. deletion criteria : (Optional) Specify the Document to delete.
  2. justOne : (Optional) If set to true or 1, only delete a Document.

Syntax

The basic syntax of remove () method is as follows:

 > db . COLLECTION_NAME . remove ( DELLETION_CRITTERIA ) 

For instance,

You monitor the Collection named mycol with the following data:

 { "_id" : ObjectId ( 5983548781331adf45ec5 ), "title" : "MongoDB Overview" } { "_id" : ObjectId ( 5983548781331adf45ec6 ), "title" : "NoSQL Overview" } { "_id" : ObjectId ( 5983548781331adf45ec7 ), "title" : "Tutorials Point Overview" } 

the example below will delete all documents whose title is 'MongoDB Overview':

 > db . mycol . remove ({ 'title' : 'MongoDB Overview' }) > db . mycol . find () { "_id" : ObjectId ( 5983548781331adf45ec6 ), "title" : "NoSQL Overview" } { "_id" : ObjectId ( 5983548781331adf45ec7 ), "title" : "Tutorials Point Overview" } > 

Only delete a Document in MongoDB

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

 > db . COLLECTION_NAME . remove ( DELETION_CRITERIA , 1 ) 

Delete all Documents in MongoDB

FAQ

What should you know about remove () method in MongoDB?

The remove () method in MongoDB helps delete the Document from Collection. The remove () method takes two parameters. The first parameter of the deletion criteria determines the Document to delete, and the second parameter is justOne.

What should you know about only delete a Document in MongoDB?

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

What is Delete Document in Mongodb?

Remove () method in MongoDB The remove () method in MongoDB helps delete the Document from Collection.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.