distinct.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.DistinctOperation = void 0;
  4. const utils_1 = require("../utils");
  5. const command_1 = require("./command");
  6. const operation_1 = require("./operation");
  7. /**
  8. * Return a list of distinct values for the given key across a collection.
  9. * @internal
  10. */
  11. class DistinctOperation extends command_1.CommandOperation {
  12. /**
  13. * Construct a Distinct operation.
  14. *
  15. * @param collection - Collection instance.
  16. * @param key - Field of the document to find distinct values for.
  17. * @param query - The query for filtering the set of documents to which we apply the distinct filter.
  18. * @param options - Optional settings. See Collection.prototype.distinct for a list of options.
  19. */
  20. constructor(collection, key, query, options) {
  21. super(collection, options);
  22. this.options = options ?? {};
  23. this.collection = collection;
  24. this.key = key;
  25. this.query = query;
  26. }
  27. async execute(server, session) {
  28. const coll = this.collection;
  29. const key = this.key;
  30. const query = this.query;
  31. const options = this.options;
  32. // Distinct command
  33. const cmd = {
  34. distinct: coll.collectionName,
  35. key: key,
  36. query: query
  37. };
  38. // Add maxTimeMS if defined
  39. if (typeof options.maxTimeMS === 'number') {
  40. cmd.maxTimeMS = options.maxTimeMS;
  41. }
  42. // we check for undefined specifically here to allow falsy values
  43. // eslint-disable-next-line no-restricted-syntax
  44. if (typeof options.comment !== 'undefined') {
  45. cmd.comment = options.comment;
  46. }
  47. // Do we have a readConcern specified
  48. (0, utils_1.decorateWithReadConcern)(cmd, coll, options);
  49. // Have we specified collation
  50. (0, utils_1.decorateWithCollation)(cmd, coll, options);
  51. const result = await super.executeCommand(server, session, cmd);
  52. return this.explain ? result : result.values;
  53. }
  54. }
  55. exports.DistinctOperation = DistinctOperation;
  56. (0, operation_1.defineAspects)(DistinctOperation, [operation_1.Aspect.READ_OPERATION, operation_1.Aspect.RETRYABLE, operation_1.Aspect.EXPLAINABLE]);
  57. //# sourceMappingURL=distinct.js.map