In MongoDB, if you need to delete a document in the mongo shell, you can use:
- The
db.collection.remove()
method - The
delete
command - The
db.collection.deleteOne()
method - The
db.collection.deleteMany()
method
This article provides examples of each.
The db.collection.remove()
Method
The db.collection.remove()
method removes (i.e. deletes) documents from a collection. You can delete all documents, some documents, or a single document as required.
Here’s an example of deleting a specific document.
db.employees.remove({ "_id": 3 })
Result:
WriteResult({ "nRemoved" : 1 })
That deletes the document with an _id
value of 3
from the employees
collection.
It doesn’t have to match a single document though. The filter criteria can match multiple documents, and you can even leave the filter criteria blank to match all documents (i.e. delete all documents).
Here’s an example of deleting all documents:
db.employees.remove({})
Result:
WriteResult({ "nRemoved" : 5 })
The result shows us that five documents were removed (so the collection obviously contained five documents before we deleted them all).
The remove()
method also accepts a justOne
parameter to limit the removal to just one document. This deletes the first document that matches the deletion criteria.
Example:
db.employees.remove({}, "justOne: true")
Result:
WriteResult({ "nRemoved" : 1 })
In this case, our filter criteria is an empty document, and so it matches all documents in the collection. However, only one document is deleted, because we use justOne: true
.
The delete
Command
The delete
command does exactly the same thing as the remove()
method. In fact, the remove()
method uses the delete command.
Here’s an example of deleting a specific document with the delete
command.
db.runCommand(
{
delete: "employees",
deletes: [ { q: { _id: 4 }, limit: 1 } ]
}
)
Result:
{ "n" : 1, "ok" : 1 }
The db.collection.deleteOne()
Method
The db.collection.deleteOne()
method is similar to the db.collection.remove()
method, except that it deletes just one document from the specified collection.
It accepts a filter condition, just like db.collection.remove()
.
Example:
db.employees.deleteOne({ "_id": 4 })
Result:
{ "acknowledged" : true, "deletedCount" : 1 }
This deleted employee number 4.
If you provide a more broad filter that returns multiple documents, it only deletes the first one.
For example, the following deletes the first document in the collection, regardless of how many documents are in the collection:
db.employees.deleteOne({})
The db.collection.deleteOne()
method can be a handy method to use if you’re worried about accidentally deleting multiple documents. The most documents it will delete is one, so if you make a “fat finger” mistake, you won’t accidentally remove all documents from the collection (unless of course there’s only one document in the collection).
The db.collection.deleteMany()
Method
The db.collection.deleteMany()
method is similar to db.collection.deleteOne()
except that it can delete multiple documents.
Actually, it’s probably more like remove()
, because it can remove multiple documents and a single document (although it doesn’t have the justOne
parameter like remove()
does).
This method can be handy if you want to delete all documents that match a given criteria.
Example:
db.employees.deleteMany({ "salary": { $gt: 80000 } })
Result:
{ "acknowledged" : true, "deletedCount" : 2 }
This deleted all documents that have a salary
field over 80000. In this case only two documents matched, but it could’ve been any number.
You can also use db.collection.deleteMany()
to delete all documents from the collection.
Example:
db.employees.deleteMany({})
Result:
{ "acknowledged" : true, "deletedCount" : 5 }
In this case there were five documents in the collection and all five were deleted.
More Information
The above methods also accept a number of optional arguments, such as writeConcern
and collation
.
deleteOne()
and deleteMany()
also accept a hint
argument.
Below are links to the MongoDB documentation for each method/command: