Removing documents

db.remove(query, options, callback) will remove all documents matching query according to options

  • query is the same as the ones used for finding and updating
  • options only one option for now: multi which allows the removal of multiple documents if set to true. Default is false
  • callback is optional, signature: err, numRemoved
  1. // Let's use the same example collection as in the "finding document" part
  2. // { _id: 'id1', planet: 'Mars', system: 'solar', inhabited: false }
  3. // { _id: 'id2', planet: 'Earth', system: 'solar', inhabited: true }
  4. // { _id: 'id3', planet: 'Jupiter', system: 'solar', inhabited: false }
  5. // { _id: 'id4', planet: 'Omicron Persia 8', system: 'futurama', inhabited: true }
  6. // Remove one document from the collection
  7. // options set to {} since the default for multi is false
  8. db.remove({ _id: 'id2' }, {}, function (err, numRemoved) {
  9. // numRemoved = 1
  10. });
  11. // Remove multiple documents
  12. db.remove({ system: 'solar' }, { multi: true }, function (err, numRemoved) {
  13. // numRemoved = 3
  14. // All planets from the solar system were removed
  15. });
  16. // Removing all documents with the 'match-all' query
  17. db.remove({}, { multi: true }, function (err, numRemoved) {
  18. });