MongoDB - Delete a Document
In MongoDB, you can delete documents using one of three methods.
MongoDB provides three methods for deleting documents:
db.collection.deleteOne()
db.collection.deleteMany()
db.collection.remove()
The db.collection.deleteOne()
Method
The db.collection.deleteOne()
deletes only one document, even if more than one document matches the criteria.
Here's an example of the db.collection.deleteOne()
method to delete a single document.
First, let's run a query that returns multiple results:
Results:
{ "_id" : ObjectId("5781d7f248ef8c6b3ffb014d"), "artistname" : "The Kooks" } { "_id" : ObjectId("5781d7f248ef8c6b3ffb014e"), "artistname" : "Bastille" } { "_id" : ObjectId("5781d7f248ef8c6b3ffb014f"), "artistname" : "Gang of Four" }
OK, so we know there are three documents that match that criteria.
Now let's use the exact same filtering criteria for our db.collection.deleteOne()
method:
Resulting message:
{ "acknowledged" : true, "deletedCount" : 1 }
So only one document was deleted, even though three documents matched the criteria.
Let's run the find()
query again to see which documents remain:
Results:
{ "_id" : ObjectId("5781d7f248ef8c6b3ffb014e"), "artistname" : "Bastille" } { "_id" : ObjectId("5781d7f248ef8c6b3ffb014f"), "artistname" : "Gang of Four" }
The db.collection.deleteMany()
Method
The db.collection.deleteMany()
method deletes all documents that match the criteria.
So let's run the db.collection.deleteMany()
method with the exact same criteria as our previous example. Remember, two records remain. Let's see if db.collection.deleteMany()
deletes both:
Resulting message:
{ "acknowledged" : true, "deletedCount" : 2 }
So the remaining two records were deleted.
The db.collection.remove()
Method
The db.collection.remove()
method deletes a single document or all documents that match the specified criteria.
Here, we delete all documents where the artist name is "AC/DC".
Results:
WriteResult({ "nRemoved" : 1 })
In this case, there's only one AC/DC :)
The justOne
Option
You can use the justOne
parameter to limit the remove operation to just one document (just like using db.collection.deleteOne()
).
Here's an example.
First, let's run a query that returns multiple documents:
Results:
{ "_id" : 2, "name" : "Ian Paice", "instrument" : "Drums", "born" : 1948 } { "_id" : 3, "name" : "Roger Glover", "instrument" : "Bass", "born" : 1945 } { "_id" : 5, "name" : "Don Airey", "instrument" : "Keyboards", "born" : 1948 }
Now we'll delete one of those records using the justOne
option. Again, we'll use the exact same filtering criteria:
Resulting message:
WriteResult({ "nRemoved" : 1 })
Now let's run the same query to see which documents are remaining:
Results:
{ "_id" : 3, "name" : "Roger Glover", "instrument" : "Bass", "born" : 1945 } { "_id" : 5, "name" : "Don Airey", "instrument" : "Keyboards", "born" : 1948 }
Delete all Documents in a Collection
You can delete all documents in a collection simply by omitting any filtering criteria.
Let's delete all documents in the artists collection:
Resulting message:
WriteResult({ "nRemoved" : 8 })
If you receive a Error: remove needs a query error, check that you haven't forgotten to include the curly braces. You still need to include these.