MongoDB remove()

In MongoDB the db.collection.remove() method removes documents from a collection. In other words, it deletes them.

The collection part is the name of the collection with which to remove the documents from.

You can delete all documents, some documents, or a single document as required.

Remove All Documents

In this example, we remove all documents from a collection called employees:

db.employees.remove({})

Result:

WriteResult({ "nRemoved" : 5 })

The result shows us that five documents were removed.

Remove Selected Documents

In this example, we remove only some documents.

First, let’s populate our employees collection again:

db.employees.insertMany([
    { _id: 1, name: "Sandy", salary: 55000 },
    { _id: 2, name: "Sarah", salary: 128000 },
    { _id: 3, name: "Fritz", salary: 25000 },
    { _id: 4, name: "Chris", salary: 45000 },
    { _id: 5, name: "Beck", salary: 82000 }
    ])

Now, let’s remove all employees that earn more than 80,000 per year.

db.employees.remove({ "salary": { $gt: 80000 } })

Result:

WriteResult({ "nRemoved" : 2 })

This time only two documents were removed.

Let’s take a look at the collection.

db.employees.find()

Result:

{ "_id" : 1, "name" : "Sandy", "salary" : 55000 }
{ "_id" : 3, "name" : "Fritz", "salary" : 25000 }
{ "_id" : 4, "name" : "Chris", "salary" : 45000 }

As expected, the two top salary earners have left the company.

Remove One Document

There are a couple of ways to remove a single document from a collection. The method you use will depend on what you need to do.

You can either:

  • Remove a specific document
  • Use the justOne parameter

Remove a Specific Document

If you need to remove a specific document, you can specify the document’s _id value to remove that specific document.

So if we wanted to remove say, Fritz from the above collection, we could specify his _id value of 3. Like this:

db.employees.remove({ "_id": 3 })

Result:

WriteResult({ "nRemoved" : 1 })

And let’s check the collection again:

db.employees.find()

Result:

{ "_id" : 1, "name" : "Sandy", "salary" : 55000 }
{ "_id" : 4, "name" : "Chris", "salary" : 45000 }

As expected, Fritz has now disappeared.

Use the justOne Parameter

You can alternatively use the justOne parameter to limit the removal to just one document. This deletes the first document that matches the deletion criteria.

To demonstrate this point as clearly as possible, let’s remove all remaining documents from the collection and then repopulate it:

db.employees.remove({})
db.employees.insertMany([
    { _id: 1, name: "Sandy", salary: 55000 },
    { _id: 2, name: "Sarah", salary: 128000 },
    { _id: 3, name: "Fritz", salary: 25000 },
    { _id: 4, name: "Chris", salary: 45000 },
    { _id: 5, name: "Beck", salary: 82000 }
    ])

OK, so we know that there are five documents in our collection.

Let’s now use the justOne parameter to remove a single document.

db.employees.remove({}, "justOne: true")

Result:

WriteResult({ "nRemoved" : 1 })

And let’s check the collection to see which document was deleted.

db.employees.find()

Result:

{ "_id" : 2, "name" : "Sarah", "salary" : 128000 }
{ "_id" : 3, "name" : "Fritz", "salary" : 25000 }
{ "_id" : 4, "name" : "Chris", "salary" : 45000 }
{ "_id" : 5, "name" : "Beck", "salary" : 82000 }

As expected, the first document was deleted.

The justOne parameter also has another syntax. You can also use it inside a document. You’ll need to do this if you’re going to pass other arguments to the method.

Here’s an example.

db.employees.remove(
    {}, 
    {
        justOne: true,
        writeConcern: { w: "majority", wtimeout: 3000 },
        collation: { locale: "de", strength: 1 }
    }
)

Result:

WriteResult({ "nRemoved" : 1 })

And let’s take another look at the collection

db.employees.find()

Result:

{ "_id" : 3, "name" : "Fritz", "salary" : 25000 }
{ "_id" : 4, "name" : "Chris", "salary" : 45000 }
{ "_id" : 5, "name" : "Beck", "salary" : 82000 }

As expected, the next document was deleted.

More Information

See the MongoDB documentation for more information.