find.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. 'use strict';
  2. const OperationBase = require('./operation').OperationBase;
  3. const Aspect = require('./operation').Aspect;
  4. const defineAspects = require('./operation').defineAspects;
  5. const ReadPreference = require('../core').ReadPreference;
  6. const maxWireVersion = require('../core/utils').maxWireVersion;
  7. const MongoError = require('../core/error').MongoError;
  8. class FindOperation extends OperationBase {
  9. constructor(collection, ns, command, options) {
  10. super(options);
  11. this.ns = ns;
  12. this.cmd = command;
  13. this.readPreference = ReadPreference.resolve(collection, this.options);
  14. }
  15. execute(server, callback) {
  16. // copied from `CommandOperationV2`, to be subclassed in the future
  17. this.server = server;
  18. // updates readPreference if setReadPreference was called on the cursor
  19. this.readPreference = ReadPreference.resolve(this, this.options);
  20. if (typeof this.cmd.allowDiskUse !== 'undefined' && maxWireVersion(server) < 4) {
  21. callback(new MongoError('The `allowDiskUse` option is not supported on MongoDB < 3.2'));
  22. return;
  23. }
  24. if (this.explain) {
  25. // We need to manually ensure explain is in the options.
  26. this.options.explain = this.explain.verbosity;
  27. }
  28. // TOOD: use `MongoDBNamespace` through and through
  29. const cursorState = this.cursorState || {};
  30. server.query(this.ns.toString(), this.cmd, cursorState, this.options, callback);
  31. }
  32. }
  33. defineAspects(FindOperation, [
  34. Aspect.READ_OPERATION,
  35. Aspect.RETRYABLE,
  36. Aspect.EXECUTE_WITH_SELECTION,
  37. Aspect.EXPLAINABLE
  38. ]);
  39. module.exports = FindOperation;