MongoDB – Delete Single Document Using MongoShell

In MongoDB, you are allowed to delete an existing document from the collection using db.collection.deleteOne() method. This method deletes a single document from the collection according to the filter.

deleteOne() is a mongo shell method, which updates one document at at time. This method can be used in the multi-document transactions.

  • If you use this method in capped collection, then it will throw an exception.
  • This method deletes the first document that satisfies the given condition in the collection. Or in other words, if multiple documents satisfy the given condition, then this method will delete the very first document that matches the given filter or condition among multiple documents. It does not delete all the documents that match the filter.

Syntax:

db.collection.deleteOne(
    <filter>,
   {
      writeConcern: <document>,
     collation: <document>
   }
)

Parameters:

fileter: First parameter of this method. It specifies the selection criteria for the delete using query operators. The type of this parameter is document. If it contains empty document, i.e, {}, then this method will delete the first document from the collection.

Optional Parameters:



  • writeConcern: It is only used when you do not want to use the default write concern. The type of this parameter is document.
  • collation: It specifies the use of the collation for operations. It allows users to specify the language-specific rules for string comparison like rules for lettercase and accent marks. The type of this parameter is document.

Return: This method will return a document that contains a boolean acknowledged as true (if the write concern is enabled) or false (if the write concern is disabled) and deletedCount that represents the total number of deleted documents.

Examples:

In the following examples, we are working with:

Database: GeeksforGeeks
Collection: contributor
Document: four documents that contain the details of the contributors in the form of field-value pairs.

Deleting first document:

In this example, we are deleting first document from the contributor collection by passing empty document in the db.collection.deleteOne() method.

 db.contributor.deleteOne({})

Deleting a single document that matches the filter:

In this example, we are deleting a single document that matches the filter, i.e., name: “Somya” from the contributor collection using db.collection.deleteOne() method. Or in other words, we are deleting the data of a contributor whose name is Somya from the database.

Deleting document:

In this example, we are deleting a document from the contributor collection. Here two document matches the filter(i.e., language: “C#”), so this method will delete the first document that matches the filter among these two documents as shown in the below image:

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.