delete_one.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. 'use strict';
  2. const OperationBase = require('./operation').OperationBase;
  3. const removeDocuments = require('./common_functions').removeDocuments;
  4. const Aspect = require('./operation').Aspect;
  5. const defineAspects = require('./operation').defineAspects;
  6. class DeleteOneOperation extends OperationBase {
  7. constructor(collection, filter, options) {
  8. super(options);
  9. this.collection = collection;
  10. this.filter = filter;
  11. }
  12. execute(callback) {
  13. const coll = this.collection;
  14. const filter = this.filter;
  15. const options = this.options;
  16. options.single = true;
  17. removeDocuments(coll, filter, options, (err, r) => {
  18. if (callback == null) return;
  19. if (err && callback) return callback(err);
  20. if (r == null) return callback(null, { result: { ok: 1 } });
  21. // If an explain operation was executed, don't process the server results
  22. if (this.explain) return callback(undefined, r.result);
  23. r.deletedCount = r.result.n;
  24. callback(null, r);
  25. });
  26. }
  27. }
  28. defineAspects(DeleteOneOperation, [Aspect.EXPLAINABLE]);
  29. module.exports = DeleteOneOperation;