Open In App

MongoDB – Delete Single Document Using MongoShell

Improve
Improve
Like Article
Like
Save
Share
Report

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 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>,
     hint: <document|string>        // Available starting in MongoDB 4.4
   }
)

Parameters: filter: 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.
  • hint:  It specifies the index to use to support the filter query

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:


Last Updated : 29 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads