find_one.js 864 B

12345678910111213141516171819202122232425262728293031323334353637
  1. 'use strict';
  2. const handleCallback = require('../utils').handleCallback;
  3. const OperationBase = require('./operation').OperationBase;
  4. const toError = require('../utils').toError;
  5. class FindOneOperation extends OperationBase {
  6. constructor(collection, query, options) {
  7. super(options);
  8. this.collection = collection;
  9. this.query = query;
  10. }
  11. execute(callback) {
  12. const coll = this.collection;
  13. const query = this.query;
  14. const options = this.options;
  15. try {
  16. const cursor = coll
  17. .find(query, options)
  18. .limit(-1)
  19. .batchSize(1);
  20. // Return the item
  21. cursor.next((err, item) => {
  22. if (err != null) return handleCallback(callback, toError(err), null);
  23. handleCallback(callback, null, item);
  24. });
  25. } catch (e) {
  26. callback(e);
  27. }
  28. }
  29. }
  30. module.exports = FindOneOperation;