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 ...

Get JavaScript JSON Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.