Removing documents
Removing documents
db.remove(query, options, callback) will remove all documents matching query according to options
queryis the same as the ones used for finding and updatingoptionsonly one option for now:multiwhich allows the removal of multiple documents if set to true. Default is falsecallbackis optional, signature: err, numRemoved
// Let's use the same example collection as in the "finding document" part
// { _id: 'id1', planet: 'Mars', system: 'solar', inhabited: false }
// { _id: 'id2', planet: 'Earth', system: 'solar', inhabited: true }
// { _id: 'id3', planet: 'Jupiter', system: 'solar', inhabited: false }
// { _id: 'id4', planet: 'Omicron Persia 8', system: 'futurama', inhabited: true }
// Remove one document from the collection
// options set to {} since the default for multi is false
db.remove({ _id: 'id2' }, {}, function (err, numRemoved) {
// numRemoved = 1
});
// Remove multiple documents
db.remove({ system: 'solar' }, { multi: true }, function (err, numRemoved) {
// numRemoved = 3
// All planets from the solar system were removed
});
// Removing all documents with the 'match-all' query
db.remove({}, { multi: true }, function (err, numRemoved) {
});
