O'Reilly logo

JavaScript JSON Cookbook by Ray Rischpater

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Deleting a document in MongoDB using Node.js

At some point, you may want to delete a document in a collection using Node.js.

How to do it...

You do this using the remove method, which removes matching documents from the collection you specify. Here's an example of how to call remove:

var remove = function(collection, callback) {
  collection.remove({ call: 'kf6gpe-7'},
    function(error, result)
    {
      console.log('remove returned ' + error);
      console.log(result);
      callback(result);
    });
};

How it works…

This code removes documents that have a call field with the value kf6gpe-7. As you may have guessed, the search criteria used for remove can be anything you'd pass to find. The remove method removes all documents matching your search criteria, so be careful! Calling ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required