Remove Data with Node.js

Overview

You can use the deleteOne method and the deleteMany method to remove documents from a collection. The method takes a conditions document that determines the documents to remove.

To specify a remove condition, use the same structure and syntax as the query conditions. See Find or Query Data with Node.js for an introduction to query conditions.

Prerequisites

The examples in this section use the restaurants collection in the test database. For instructions on populating the collection with the sample dataset, see Import Example Dataset.

In the node shell, define the following variables to access the required modules as well as to initialize url to the MongoDB uri.

var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var ObjectId = require('mongodb').ObjectID;
var url = 'mongodb://localhost:27017/test';

Procedures

Remove All Documents That Match a Condition

The following operation removes all documents that match the specified condition.

Define a removeRestaurants function as follows:

var removeRestaurants = function(db, callback) {
   db.collection('restaurants').deleteMany(
      { "borough": "Manhattan" },
      function(err, results) {
         console.log(results);
         callback();
      }
   );
};

Call the removeRestaurants function.

MongoClient.connect(url, function(err, db) {
  assert.equal(null, err);

  removeRestaurants(db, function() {
      db.close();
  });
});

The operation prints to the console the number of documents removed.

Remove at Most a Single Document

Use the deleteOne method to delete at most one matching document.

Define a removeRestaurants function as follows:

var removeRestaurants = function(db, callback) {
   db.collection('restaurants').deleteOne(
      { "borough": "Queens" },
      function(err, results) {
         console.log(results);
         callback();
      }
   );
};

Call the removeRestaurants function.

MongoClient.connect(url, function(err, db) {
  assert.equal(null, err);

  removeRestaurants(db, function() {
      db.close();
  });
});

The operation prints to the console the number of documents removed.

Remove All Documents

To remove all documents from a collection, pass an empty conditions document {} to the deleteMany method.

Define a removeRestaurants function as follows:

var removeRestaurants = function(db, callback) {
   db.collection('restaurants').deleteMany( {}, function(err, results) {
      console.log(results);
      callback();
   });
};

Call the removeRestaurants function.

MongoClient.connect(url, function(err, db) {
  assert.equal(null, err);

  removeRestaurants(db, function() {
      db.close();
  });
});

The operation prints to the console the number of documents removed.

Drop a Collection

The remove all operation only removes the documents from the collection. The collection itself, as well as any indexes for the collection, remain. To remove all documents from a collection, it may be more efficient to drop the entire collection, including the indexes, and then recreate the collection and rebuild the indexes. Use the drop method to drop a collection, including any indexes.

Define a dropRestaurants function as follows:

var dropRestaurants = function(db, callback) {
   db.collection('restaurants').drop( function(err, response) {
      console.log(response)
      callback();
   });
};

Call the dropRestaurants function.

MongoClient.connect(url, function(err, db) {
  assert.equal(null, err);

  dropRestaurants(db, function() {
      db.close();
  });
});

Upon successful drop of the collection, the operation prints to the console true. If the collection to drop does not exist, the operation prints to the console false.

Additional Information

In the Node.js documentation, see deleteOne, deleteMany and drop.

In MongoDB, write operations are atomic on the level of a single document. If a single remove operation removes multiple documents from a collection, the operation can interleave with other write operations on that collection. In the MongoDB Manual, see Atomicity.