Delete MongoDB Document

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

When you store and manage your data in MongoDB, there will be times when you need to delete one or more documents from a collection. Fortunately, this task is easy to accomplish with a simple command. In this article, we’ll show you how to delete a MongoDB document and provide examples that cover a variety of situations.

Prerequisite

Before proceeding with this tutorial, make sure that MongoDB is installed and configured. If you need to download the software, you can do so here.

Delete a MongoDB Document Using Remove() Method

The remove() method is used to remove documents that match a specified query expression. The basic syntax can be seen below:

1
db.collection.remove()

In our examples, we’ll be using a MongoDB collection named person. It will contain the following documents:

1
2
3
4
5
{ "_id" : ObjectId("5e92c61b88c6e1729447f926"), "name" : "rommel", "lastname" : "galisanao", "age" : 38 }
{ "_id" : ObjectId("5e92c61b88c6e1729447f927"), "name" : "risa", "lastname" : "dira", "age" : 38 }
{ "_id" : ObjectId("5e92c61b88c6e1729447f928"), "name" : "jasmine", "lastname" : "diaz", "age" : 16 }
{ "_id" : ObjectId("5e92c61b88c6e1729447f929"), "name" : "luffy", "lastname" : "zamora", "age" : 9 }
{ "_id" : ObjectId("5e92c61b88c6e1729447f92a"), "name" : "robin", "lastname" : "dales", "age" : 3 }

If you’d like to follow along with this tutorial on your own machine, you can create your own sample collection containing similar documents.

Let’s say that we want to remove a person from the collection where the value of age was 3. Our remove command will look like this:

1
db.person.remove({age:3})

We should see output that looks like the following:

1
WriteResult({ "nRemoved" : 1 })

While the output tells us that one document was removed, we can still confirm that the specified document was deleted using the following query:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
> db.person.find().pretty();
{
        "_id" : ObjectId("5e92c61b88c6e1729447f926"),
        "name" : "rommel",
        "lastname" : "galisanao",
        "age" : 38
}
{
        "_id" : ObjectId("5e92c61b88c6e1729447f927"),
        "name" : "risa",
        "lastname" : "dira",
        "age" : 38
}
{
        "_id" : ObjectId("5e92c61b88c6e1729447f928"),
        "name" : "jasmine",
        "lastname" : "diaz",
        "age" : 16
}
{
        "_id" : ObjectId("5e92c61b88c6e1729447f929"),
        "name" : "luffy",
        "lastname" : "zamora",
        "age" : 9
}

Delete a Single MongoDB Document

What if we only wanted to delete a single document that matches specified criteria, even if there are multiple documents that match the given criteria? To do this, we use the parameter justOne. In MongoDB, justOne is a Boolean parameter that accepts 1 or 0, where 1 is equivalent to limiting the deletion to one document only.

The basic syntax is: db.collection.remove(<criteria>).

Using the person collection shown below, let’s delete a person whose age is 38. Notice that we have two documents matching that criteria:

1
2
3
4
{ "_id" : ObjectId("5e92c61b88c6e1729447f926"), "name" : "rommel", "lastname" : "galisanao", "age" : 38 }
{ "_id" : ObjectId("5e92c61b88c6e1729447f927"), "name" : "risa", "lastname" : "dira", "age" : 38 }
{ "_id" : ObjectId("5e92c61b88c6e1729447f928"), "name" : "jasmine", "lastname" : "diaz", "age" : 16 }
{ "_id" : ObjectId("5e92c61b88c6e1729447f929"), "name" : "luffy", "lastname" : "zamora", "age" : 9 }

We’ll execute our remove command, supplying a value of 1 for the justOne parameter:

1
db.person.remove({"age": 38},1)

The output should look like this:

1
WriteResult({ "nRemoved" : 1 })

Let’s verify that we actually removed a single document using the command shown below:

1
2
3
4
> db.person.find();
{ "_id" : ObjectId("5e92c61b88c6e1729447f927"), "name" : "risa", "lastname" : "dira", "age" : 38 }
{ "_id" : ObjectId("5e92c61b88c6e1729447f928"), "name" : "jasmine", "lastname" : "diaz", "age" : 16 }
{ "_id" : ObjectId("5e92c61b88c6e1729447f929"), "name" : "luffy", "lastname" : "zamora", "age" : 9 }

Notice that the first document that matched the criteria was deleted.

NOTE: It’s possible to remove all the documents in a collection without dropping the collection by using the following command: db.collection.remove({}).

Conclusion

When you store data in a MongoDB collection, you may find that you need to delete one or more documents at some point. The remove() command makes it easy to remove documents that meet specified query criteria. In this article, we showed you how to delete a MongoDB document from a collection, and we provided examples that illustrate how to use the command in different ways. With this tutorial to guide you, you’ll be prepared to delete documents from your own MongoDB collecrtions.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.