distinct.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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.CommandCallbackOperation {
  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. executeCallback(server, session, callback) {
  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. try {
  51. (0, utils_1.decorateWithCollation)(cmd, coll, options);
  52. }
  53. catch (err) {
  54. return callback(err);
  55. }
  56. super.executeCommandCallback(server, session, cmd, (err, result) => {
  57. if (err) {
  58. callback(err);
  59. return;
  60. }
  61. callback(undefined, this.explain ? result : result.values);
  62. });
  63. }
  64. }
  65. exports.DistinctOperation = DistinctOperation;
  66. (0, operation_1.defineAspects)(DistinctOperation, [operation_1.Aspect.READ_OPERATION, operation_1.Aspect.RETRYABLE, operation_1.Aspect.EXPLAINABLE]);
  67. //# sourceMappingURL=distinct.js.map