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 remove({})
removes all of the documents in the current collection.
The remove
method returns a count of the number of items removed from the collection.
See also
For more information about MongoDB's remove method,...