distinct.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.DistinctOperation = void 0;
  4. const error_1 = require("../error");
  5. const utils_1 = require("../utils");
  6. const command_1 = require("./command");
  7. const operation_1 = require("./operation");
  8. /**
  9. * Return a list of distinct values for the given key across a collection.
  10. * @internal
  11. */
  12. class DistinctOperation extends command_1.CommandOperation {
  13. /**
  14. * Construct a Distinct operation.
  15. *
  16. * @param collection - Collection instance.
  17. * @param key - Field of the document to find distinct values for.
  18. * @param query - The query for filtering the set of documents to which we apply the distinct filter.
  19. * @param options - Optional settings. See Collection.prototype.distinct for a list of options.
  20. */
  21. constructor(collection, key, query, options) {
  22. super(collection, options);
  23. this.options = options !== null && options !== void 0 ? options : {};
  24. this.collection = collection;
  25. this.key = key;
  26. this.query = query;
  27. }
  28. execute(server, session, callback) {
  29. const coll = this.collection;
  30. const key = this.key;
  31. const query = this.query;
  32. const options = this.options;
  33. // Distinct command
  34. const cmd = {
  35. distinct: coll.collectionName,
  36. key: key,
  37. query: query
  38. };
  39. // Add maxTimeMS if defined
  40. if (typeof options.maxTimeMS === 'number') {
  41. cmd.maxTimeMS = options.maxTimeMS;
  42. }
  43. // Do we have a readConcern specified
  44. (0, utils_1.decorateWithReadConcern)(cmd, coll, options);
  45. // Have we specified collation
  46. try {
  47. (0, utils_1.decorateWithCollation)(cmd, coll, options);
  48. }
  49. catch (err) {
  50. return callback(err);
  51. }
  52. if (this.explain && (0, utils_1.maxWireVersion)(server) < 4) {
  53. callback(new error_1.MongoCompatibilityError(`Server ${server.name} does not support explain on distinct`));
  54. return;
  55. }
  56. super.executeCommand(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