count.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.CountOperation = void 0;
  4. const responses_1 = require("../cmap/wire_protocol/responses");
  5. const command_1 = require("./command");
  6. const operation_1 = require("./operation");
  7. /** @internal */
  8. class CountOperation extends command_1.CommandOperation {
  9. constructor(namespace, filter, options) {
  10. super({ s: { namespace: namespace } }, options);
  11. this.SERVER_COMMAND_RESPONSE_TYPE = responses_1.MongoDBResponse;
  12. this.options = options;
  13. this.collectionName = namespace.collection;
  14. this.query = filter;
  15. }
  16. get commandName() {
  17. return 'count';
  18. }
  19. buildCommandDocument(_connection, _session) {
  20. const options = this.options;
  21. const cmd = {
  22. count: this.collectionName,
  23. query: this.query
  24. };
  25. if (typeof options.limit === 'number') {
  26. cmd.limit = options.limit;
  27. }
  28. if (typeof options.skip === 'number') {
  29. cmd.skip = options.skip;
  30. }
  31. if (options.hint != null) {
  32. cmd.hint = options.hint;
  33. }
  34. if (typeof options.maxTimeMS === 'number') {
  35. cmd.maxTimeMS = options.maxTimeMS;
  36. }
  37. return cmd;
  38. }
  39. handleOk(response) {
  40. return response.getNumber('n') ?? 0;
  41. }
  42. }
  43. exports.CountOperation = CountOperation;
  44. (0, operation_1.defineAspects)(CountOperation, [operation_1.Aspect.READ_OPERATION, operation_1.Aspect.RETRYABLE, operation_1.Aspect.SUPPORTS_RAW_DATA]);
  45. //# sourceMappingURL=count.js.map