Delete Document in MongoDB

Remove () method in MongoDB

The remove () method in MongoDB is used to 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 example

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 following example 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

If you do not specify the deletion criteria, MongoDB will delete all documents from Collection.This is equivalent to the truncate command in SQL .

 > db . mycol . remove () > db . mycol . find () > 

According to Tutorialspoint

Previous article: Update Document in MongoDB

Next article: Projection in MongoDB

4 ★ | 1 Vote

May be interested

  • Instructions on 2 ways to install MongoDB on Raspberry PiInstructions on 2 ways to install MongoDB on Raspberry Pi
    in this tutorial, tipsmake will guide you through the process of installing and setting up the mongodb server software on your raspberry pi.
  • Reference Database in MongoDBReference Database in MongoDB
    as shown in the relationship chapter in mongodb, to deploy a standardized database structure in mongodb, we use the referenced relationship concept, also known as manual references, in which we manipulate to store the id of the documents referenced in another document.
  • Delete Database in MongoDBDelete Database in MongoDB
    the db.dropdatabase () command in mongodb is used to delete an existing database.
  • Limit records in MongoDBLimit records in MongoDB
    to limit the records in mongodb, you need to use the limit () method. the limit () method takes a parameter in a numeric format, which is the document number you want to display.
  • Advantages of MongoDBAdvantages of MongoDB
    any relation database has a unique schema design to index the data tables and relationships between those tables. meanwhile in mongodb there is no concept of relationship.
  • Data type in MongoDBData type in MongoDB
    mongodb supports many different data types. the following are some typical data types.
  • MongoDB malicious code attacks more than 26,000 victims in a weekMongoDB malicious code attacks more than 26,000 victims in a week
    malware that attacks the mongodb database has rekindled last week and after the weekend with the arrival of three new groups hijack more than 26,000 servers, of which one group attacked 22,000 machines.
  • Covered Query in MongoDBCovered Query in MongoDB
    when all fields in the query are part of the index, mongodb connects query conditions and returns the result by using the same index without looking inside the document. when indexes are present in ram, retrieving data from indexes is faster when compared to retrieving data by scanning all documents.
  • Learn about security features and authentication in MongoDBLearn about security features and authentication in MongoDB
    in the following article, we will continue to introduce security and authentication features in the mongodb database management system series. in essence, the basic account validation feature is available in mongodb, but has been disabled in default mode, and in this mode the system requires at least 1 active database ...
  • Insert Document in MongoDBInsert Document in MongoDB
    to insert data into collection in mongodb, you need to use the insert () or save () method.