estimated_document_count.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. 'use strict';
  2. const Aspect = require('./operation').Aspect;
  3. const defineAspects = require('./operation').defineAspects;
  4. const CommandOperationV2 = require('./command_v2');
  5. class EstimatedDocumentCountOperation extends CommandOperationV2 {
  6. constructor(collection, query, options) {
  7. if (typeof options === 'undefined') {
  8. options = query;
  9. query = undefined;
  10. }
  11. super(collection, options);
  12. this.collectionName = collection.s.namespace.collection;
  13. if (query) {
  14. this.query = query;
  15. }
  16. }
  17. execute(server, callback) {
  18. const options = this.options;
  19. const cmd = { count: this.collectionName };
  20. if (this.query) {
  21. cmd.query = this.query;
  22. }
  23. if (typeof options.skip === 'number') {
  24. cmd.skip = options.skip;
  25. }
  26. if (typeof options.limit === 'number') {
  27. cmd.limit = options.limit;
  28. }
  29. if (options.hint) {
  30. cmd.hint = options.hint;
  31. }
  32. super.executeCommand(server, cmd, (err, response) => {
  33. if (err) {
  34. callback(err);
  35. return;
  36. }
  37. callback(null, response.n);
  38. });
  39. }
  40. }
  41. defineAspects(EstimatedDocumentCountOperation, [
  42. Aspect.READ_OPERATION,
  43. Aspect.RETRYABLE,
  44. Aspect.EXECUTE_WITH_SELECTION
  45. ]);
  46. module.exports = EstimatedDocumentCountOperation;