aggregate.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.AggregateOperation = exports.DB_AGGREGATE_COLLECTION = 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. /** @internal */
  9. exports.DB_AGGREGATE_COLLECTION = 1;
  10. const MIN_WIRE_VERSION_$OUT_READ_CONCERN_SUPPORT = 8;
  11. /** @internal */
  12. class AggregateOperation extends command_1.CommandOperation {
  13. constructor(ns, pipeline, options) {
  14. super(undefined, { ...options, dbName: ns.db });
  15. this.options = options !== null && options !== void 0 ? options : {};
  16. // Covers when ns.collection is null, undefined or the empty string, use DB_AGGREGATE_COLLECTION
  17. this.target = ns.collection || exports.DB_AGGREGATE_COLLECTION;
  18. this.pipeline = pipeline;
  19. // determine if we have a write stage, override read preference if so
  20. this.hasWriteStage = false;
  21. if (typeof (options === null || options === void 0 ? void 0 : options.out) === 'string') {
  22. this.pipeline = this.pipeline.concat({ $out: options.out });
  23. this.hasWriteStage = true;
  24. }
  25. else if (pipeline.length > 0) {
  26. const finalStage = pipeline[pipeline.length - 1];
  27. if (finalStage.$out || finalStage.$merge) {
  28. this.hasWriteStage = true;
  29. }
  30. }
  31. if (this.hasWriteStage) {
  32. this.trySecondaryWrite = true;
  33. }
  34. if (this.explain && this.writeConcern) {
  35. throw new error_1.MongoInvalidArgumentError('Option "explain" cannot be used on an aggregate call with writeConcern');
  36. }
  37. if ((options === null || options === void 0 ? void 0 : options.cursor) != null && typeof options.cursor !== 'object') {
  38. throw new error_1.MongoInvalidArgumentError('Cursor options must be an object');
  39. }
  40. }
  41. get canRetryRead() {
  42. return !this.hasWriteStage;
  43. }
  44. addToPipeline(stage) {
  45. this.pipeline.push(stage);
  46. }
  47. execute(server, session, callback) {
  48. const options = this.options;
  49. const serverWireVersion = (0, utils_1.maxWireVersion)(server);
  50. const command = { aggregate: this.target, pipeline: this.pipeline };
  51. if (this.hasWriteStage && serverWireVersion < MIN_WIRE_VERSION_$OUT_READ_CONCERN_SUPPORT) {
  52. this.readConcern = undefined;
  53. }
  54. if (serverWireVersion >= 5) {
  55. if (this.hasWriteStage && this.writeConcern) {
  56. Object.assign(command, { writeConcern: this.writeConcern });
  57. }
  58. }
  59. if (options.bypassDocumentValidation === true) {
  60. command.bypassDocumentValidation = options.bypassDocumentValidation;
  61. }
  62. if (typeof options.allowDiskUse === 'boolean') {
  63. command.allowDiskUse = options.allowDiskUse;
  64. }
  65. if (options.hint) {
  66. command.hint = options.hint;
  67. }
  68. if (options.let) {
  69. command.let = options.let;
  70. }
  71. command.cursor = options.cursor || {};
  72. if (options.batchSize && !this.hasWriteStage) {
  73. command.cursor.batchSize = options.batchSize;
  74. }
  75. super.executeCommand(server, session, command, callback);
  76. }
  77. }
  78. exports.AggregateOperation = AggregateOperation;
  79. (0, operation_1.defineAspects)(AggregateOperation, [
  80. operation_1.Aspect.READ_OPERATION,
  81. operation_1.Aspect.RETRYABLE,
  82. operation_1.Aspect.EXPLAINABLE,
  83. operation_1.Aspect.CURSOR_CREATING
  84. ]);
  85. //# sourceMappingURL=aggregate.js.map