How to Delete Documents with Mongoose

Introduction

If you’re using the popular Schema/Model based library Mongoose to interact with MongoJS from NodeJS then this article will help you with a variety of options to delete documents. Whether you need to delete all documents in a collection, a single document by id, or delete documents that match a specific condition this tutorial should give you an idea of your options so you can pick the best course of action for your deletion.

Prerequisites

  • You should have NodeJS installed.
  • You should have Mongoose installed.
  • Some command line experience is recommended.

Demo Examples

For all of our examples we will be using a model called ProductModel so you should modify any queries with your Model name.

Note: If you’re not used to dealing with Schemas/Models, this is how Mongoose deals with database interactions and you’ll want to review their documentation and familiarize yourself with the approach before proceeding.

Delete Many Documents

If you plan on deleting multiple documents based on one or multiple conditions you’ll want to use the Model.deleteMany() function. Let’s look at it’s function definition:

Model.deleteMany()
Parameters
conditions (Object)
[options] (Object) optional see Query.prototype.setOptions()
[callback] (Function)

Let’s look at an example of how we’d use it to delete all the documents with the brand ‘Nike’.

ProductModel.deleteMany({ brand: 'Nike' }, function (err) {
  if(err) console.log(err);
  console.log("Successful deletion");
});

As you can see we used the conditions parameter to detail which documents we wanted to delete and we used the callback function to do something on error and otherwise output a success message. This will delete all documents with a field brand that matches Nike.

Delete One Document

Mongoose also provides a Model.deleteOne() method to delete only the first document that matches the condition you specify. Let’s take a look at the function definition followed by an example.

Model.deleteOne()
Parameters
conditions (Object)
[callback] (Function)

Let’s demo an example of how to delete the first document that has brand equal to Nike. Again we use the callback function to log what happened.

ProductModel.deleteOne({ brand: 'Nike' }, function (err) {
  if(err) console.log(err);
  console.log("Successful deletion");
});

Find One and Delete

This next command is very similar to the above Model.deleteOne() command with a few more options. The command is Model.findOneAndDelete() but as you can see by the function definition has more parameters/options available.

Model.findOneAndDelete()
Parameters
conditions (Object)
[options] (Object)
[options.strict] (Boolean|String)
[callback] (Function)

Let’s take a look at an example:

ProductModel.findOneAndDelete({ brand: 'Nike' }, function (err) {
  if(err) console.log(err);
  console.log("Successful deletion");
});

Delete by Id

There is also the option to delete by id which is an extremely common use case. Let’s take a look a function definition then an example.

Note: This function under the hood is a findOneAndDelete() call that we discussed above given the id as the condition.

Model.findByIdAndDelete()
Parameters
id (Object|Number|String) value of _id to query by
[options] (Object) optional see Query.prototype.setOptions()
[options.strict] (Boolean|String) overwrites the schema's strict mode option
[callback] (Function)

Now let’s look at an example:

ProductModel.findByIdAndDelete(id, function (err) {
  if(err) console.log(err);
  console.log("Successful deletion");
});

Conclusion

We hope this short tutorial acquainted you with the Mongoose syntax and functionality for deleting documents in MongoDB. We hope you can apply what you saw here to your specific application. Please test your commands before deleting critical data. If you have any questions or feedback please don’t hesitate to reach out to us.

Pilot the ObjectRocket platform free for 30 Days

It's easy to get started. Imagine the time you'll save by not worrying about database management. Let's do this!

PILOT FREE FOR 30 DAYS

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.