nodejs
  1. nodejs-mongodb-remove

Node.js MongoDB Remove

In Node.js, the MongoDB driver provides several methods to delete a document or documents from a collection. In this tutorial, we'll explore how to use the remove method to remove documents from a MongoDB collection.

Syntax

The syntax to use the remove method for removing documents in a MongoDB collection is as follows:

db.collection('collectionName').remove(filter, [options], [callback]);

Example

Let's consider an example where we have a collection named "employees" and we want to remove all the documents that match a certain condition. Here's an example that demonstrates the use of the remove method in Node.js:

const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017/myproject';

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  const dbo = db.db('myproject');
  const query = { age: { $gte: 25 } };
  dbo.collection('employees').remove(query, function(err, obj) {
    if (err) throw err;
    console.log(obj.result.n + ' document(s) deleted.');
    db.close();
  });
});

In the above example, we are using the remove method to delete all the documents that match a certain query. We've passed an object to the remove() method which specifies the filter for the documents we want to remove. In this case, we're deleting documents where the "age" field is greater than or equal to 25.

The remove method has a callback function which gets called when the operation is complete. It takes two arguments, the error (if any), and a result object that contains the number of documents that were deleted.

Output

When we run the above example, we'll see the output as follows:

2 document(s) deleted.

This output tells us that 2 documents were deleted from the collection named "employees" and that the operation was successful.

Explanation

In the example above, we first connect to the MongoDB database using MongoClient. Then we get the reference of the "myproject" database and pass the filter object to remove() method of the collection "employees" to remove the documents. We also specify a callback function that prints the result of the operation and closes the database connection.

Use

The remove method is used to delete one or more documents from a MongoDB collection that match the specified filter. It takes two arguments:

  1. A filter object to specify which documents to remove
  2. An optional options object that can specify additional options such as single or multiple document deletions.

Important Points

  • The remove method deletes all the documents that match the filter object.
  • If the filter object is empty, it will delete all documents in the collection.
  • The remove method only deletes one document by default, but the justOne flag can be set to true to delete only one document, regardless of how many match the filter.
  • The remove method is inherently unsafe if not used correctly, always be sure to double-check the filter being used.

Summary

In this tutorial, we covered how to use the remove() method in Node.js to remove one or more documents from a MongoDB collection. We explored the syntax and usage of remove, discussed an example, explained its output and provided important points to keep in mind while using remove method. With the help of this tutorial, you can now efficiently use the MongoDB remove() method to remove documents that match specific filter criteria.

Published on: