How to Use mongoose remove

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

Introduction

If you need to delete data from your MongoDB database, it’s important to proceed with caution. Deleting data is a permanent operation, so there’s no real margin for error. Mongoose, the Javascript framework commonly used to communicate with MongoDB from a Node.JS application, provides a number of ways to handle deletions. It offers two delete methods that can be used to delete documents from a MongoDB collection: deleteOne() and deleteMany(). Mongoose also provides another method known as the remove() method, which can delete one or multiple documents. The remove() method is unique in that it sends a remove command directly to MongoDB with no Mongoose documents involved. This means that Mongoose does not execute document middleware for this method. In this article, we’ll look at some examples of the Mongoose remove method and learn how to use it to remove documents from a MongoDB collection.

MongoDB Dataset

Let’s start by examining the dataset we’ll use in our examples. If you’d like to follow along with our code examples, you may want to create a collection containing similar data. Our sample collection will include the following documents:

1
2
3
4
5
6
{ "_id" : ObjectId("5e463e67e6f8b824961e87cc"), "name" : "Leonel Messi", "club" : "Barcelona", "country" : "Argentina" }
{ "_id" : ObjectId("5e463ea3e6f8b824961e87cd"), "name" : "Cristiano Ronaldo", "club" : "Juventus", "country" : "Portugal" }
{ "_id" : ObjectId("5e463eb3e6f8b824961e87ce"), "name" : "Neymar", "club" : "PSG", "country" : "Brazil" }
{ "_id" : ObjectId("5e463ecee6f8b824961e87cf"), "name" : "Sergio Ramos", "club" : "Real Madrid", "country" : "Spain" }
{ "_id" : ObjectId("5e463ef9e6f8b824961e87d0"), "name" : "Marc-Andre ter Stegen", "club" : "Barcelona", "country" : "Germany" }
{ "_id" : ObjectId("5e4ce5378795c8cd70fb295a"), "name" : "Sadio Mane", "club" : "Liverpool", "country" : "Senegal" }

The name of this collection is “myteam”. In our examples, we’ll remove data from this collection using the Mongoose remove method.

We’ll also use the Postman tool to perform HTTP endpoint testing. You can download the postman tool here.

Mongoose remove

Our first task will be to create a route handler. We’ll use the code shown below:

1
router.route("/remove").delete(function(req, res) {});

This route handler will be invoked whenever the route ‘/remove’ is executed. Let’s add the remove() method to our code:

1
2
3
4
5
6
7
myteam.remove({}, function(err, result) {
  if (err) {
    console.err(err);
  } else {
    res.json(result);
  }
});

Currently, we’re passing an empty object as a query. Our first example will show how to remove more than one document. In the myteam collection, there are two documents where the value of the club field is “Barcelona”. Let’s remove them:

1
{ "club" : "Barcelona" }

The query shown above will do the job and remove those two matching documents. Let’s pass the query to the remove() method:

1
2
3
4
5
6
7
myteam.remove({ club: "Barcelona" }, function(err, result) {
  if (err) {
    console.err(err);
  } else {
    res.json(result);
  }
});

Now, let’s add this code to the route handler and execute the route using the Postman tool:

1
2
3
4
5
6
7
8
9
router.route("/remove").delete(function(req, res) {
  myteam.remove({ club: "Barcelona" }, function(err, result) {
    if (err) {
      console.err(err);
    } else {
      res.json(result);
    }
  });
});

Image from Gyazo

The remove() method returns an object. Notice that the value of deletedCount is “2”. This lets us know that two documents were deleted, but we can also verify that the deletion was successful through the Mongo shell:

Image from Gyazo

We can see that there are no longer any documents where the value of the club field is “Barcelona”.

Conclusion

If you’re using Mongoose to interact with a MongoDB database from your Node.JS applications, it’s important to understand how to handle deletions. There are a few different ways to perform delete operations in Mongoose: deleteOne() to delete a single document, deleteMany() to delete multiple documents and remove() to delete one or multiple documents. In this article, we focused on the remove() method and looked at an example to learn how it works. Our example showed how to delete multiple documents using remove(), but the Mongoose remove method can also be used to delete a single document. While there’s no doubt that the remove() method is useful, keep in mind that the method is deprecated, so you may want to consider utilizing one of the alternatives in its place.

Image from Gyazo

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.