distinct.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.DistinctOperation = void 0;
  4. const responses_1 = require("../cmap/wire_protocol/responses");
  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.SERVER_COMMAND_RESPONSE_TYPE = responses_1.MongoDBResponse;
  23. this.options = options ?? {};
  24. this.collection = collection;
  25. this.key = key;
  26. this.query = query;
  27. }
  28. get commandName() {
  29. return 'distinct';
  30. }
  31. buildCommandDocument(_connection) {
  32. const command = {
  33. distinct: this.collection.collectionName,
  34. key: this.key,
  35. query: this.query
  36. };
  37. // we check for undefined specifically here to allow falsy values
  38. // eslint-disable-next-line no-restricted-syntax
  39. if (this.options.comment !== undefined) {
  40. command.comment = this.options.comment;
  41. }
  42. if (this.options.hint != null) {
  43. command.hint = this.options.hint;
  44. }
  45. return command;
  46. }
  47. handleOk(response) {
  48. if (this.explain) {
  49. return response.toObject(this.bsonOptions);
  50. }
  51. return response.toObject(this.bsonOptions).values;
  52. }
  53. }
  54. exports.DistinctOperation = DistinctOperation;
  55. (0, operation_1.defineAspects)(DistinctOperation, [
  56. operation_1.Aspect.READ_OPERATION,
  57. operation_1.Aspect.RETRYABLE,
  58. operation_1.Aspect.EXPLAINABLE,
  59. operation_1.Aspect.SUPPORTS_RAW_DATA
  60. ]);
  61. //# sourceMappingURL=distinct.js.map