aggregation_cursor.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.AggregationCursor = void 0;
  4. const aggregate_1 = require("../operations/aggregate");
  5. const execute_operation_1 = require("../operations/execute_operation");
  6. const utils_1 = require("../utils");
  7. const abstract_cursor_1 = require("./abstract_cursor");
  8. /** @internal */
  9. const kPipeline = Symbol('pipeline');
  10. /** @internal */
  11. const kOptions = Symbol('options');
  12. /**
  13. * The **AggregationCursor** class is an internal class that embodies an aggregation cursor on MongoDB
  14. * allowing for iteration over the results returned from the underlying query. It supports
  15. * one by one document iteration, conversion to an array or can be iterated as a Node 4.X
  16. * or higher stream
  17. * @public
  18. */
  19. class AggregationCursor extends abstract_cursor_1.AbstractCursor {
  20. /** @internal */
  21. constructor(client, namespace, pipeline = [], options = {}) {
  22. super(client, namespace, options);
  23. this[kPipeline] = pipeline;
  24. this[kOptions] = options;
  25. }
  26. get pipeline() {
  27. return this[kPipeline];
  28. }
  29. clone() {
  30. const clonedOptions = (0, utils_1.mergeOptions)({}, this[kOptions]);
  31. delete clonedOptions.session;
  32. return new AggregationCursor(this.client, this.namespace, this[kPipeline], {
  33. ...clonedOptions
  34. });
  35. }
  36. map(transform) {
  37. return super.map(transform);
  38. }
  39. /** @internal */
  40. _initialize(session, callback) {
  41. const aggregateOperation = new aggregate_1.AggregateOperation(this.namespace, this[kPipeline], {
  42. ...this[kOptions],
  43. ...this.cursorOptions,
  44. session
  45. });
  46. (0, execute_operation_1.executeOperation)(this.client, aggregateOperation, (err, response) => {
  47. if (err || response == null)
  48. return callback(err);
  49. // TODO: NODE-2882
  50. callback(undefined, { server: aggregateOperation.server, session, response });
  51. });
  52. }
  53. /** Execute the explain for the cursor */
  54. async explain(verbosity) {
  55. return (0, execute_operation_1.executeOperation)(this.client, new aggregate_1.AggregateOperation(this.namespace, this[kPipeline], {
  56. ...this[kOptions],
  57. ...this.cursorOptions,
  58. explain: verbosity ?? true
  59. }));
  60. }
  61. group($group) {
  62. (0, abstract_cursor_1.assertUninitialized)(this);
  63. this[kPipeline].push({ $group });
  64. return this;
  65. }
  66. /** Add a limit stage to the aggregation pipeline */
  67. limit($limit) {
  68. (0, abstract_cursor_1.assertUninitialized)(this);
  69. this[kPipeline].push({ $limit });
  70. return this;
  71. }
  72. /** Add a match stage to the aggregation pipeline */
  73. match($match) {
  74. (0, abstract_cursor_1.assertUninitialized)(this);
  75. this[kPipeline].push({ $match });
  76. return this;
  77. }
  78. /** Add an out stage to the aggregation pipeline */
  79. out($out) {
  80. (0, abstract_cursor_1.assertUninitialized)(this);
  81. this[kPipeline].push({ $out });
  82. return this;
  83. }
  84. /**
  85. * Add a project stage to the aggregation pipeline
  86. *
  87. * @remarks
  88. * In order to strictly type this function you must provide an interface
  89. * that represents the effect of your projection on the result documents.
  90. *
  91. * By default chaining a projection to your cursor changes the returned type to the generic {@link Document} type.
  92. * You should specify a parameterized type to have assertions on your final results.
  93. *
  94. * @example
  95. * ```typescript
  96. * // Best way
  97. * const docs: AggregationCursor<{ a: number }> = cursor.project<{ a: number }>({ _id: 0, a: true });
  98. * // Flexible way
  99. * const docs: AggregationCursor<Document> = cursor.project({ _id: 0, a: true });
  100. * ```
  101. *
  102. * @remarks
  103. * In order to strictly type this function you must provide an interface
  104. * that represents the effect of your projection on the result documents.
  105. *
  106. * **Note for Typescript Users:** adding a transform changes the return type of the iteration of this cursor,
  107. * it **does not** return a new instance of a cursor. This means when calling project,
  108. * you should always assign the result to a new variable in order to get a correctly typed cursor variable.
  109. * Take note of the following example:
  110. *
  111. * @example
  112. * ```typescript
  113. * const cursor: AggregationCursor<{ a: number; b: string }> = coll.aggregate([]);
  114. * const projectCursor = cursor.project<{ a: number }>({ _id: 0, a: true });
  115. * const aPropOnlyArray: {a: number}[] = await projectCursor.toArray();
  116. *
  117. * // or always use chaining and save the final cursor
  118. *
  119. * const cursor = coll.aggregate().project<{ a: string }>({
  120. * _id: 0,
  121. * a: { $convert: { input: '$a', to: 'string' }
  122. * }});
  123. * ```
  124. */
  125. project($project) {
  126. (0, abstract_cursor_1.assertUninitialized)(this);
  127. this[kPipeline].push({ $project });
  128. return this;
  129. }
  130. /** Add a lookup stage to the aggregation pipeline */
  131. lookup($lookup) {
  132. (0, abstract_cursor_1.assertUninitialized)(this);
  133. this[kPipeline].push({ $lookup });
  134. return this;
  135. }
  136. /** Add a redact stage to the aggregation pipeline */
  137. redact($redact) {
  138. (0, abstract_cursor_1.assertUninitialized)(this);
  139. this[kPipeline].push({ $redact });
  140. return this;
  141. }
  142. /** Add a skip stage to the aggregation pipeline */
  143. skip($skip) {
  144. (0, abstract_cursor_1.assertUninitialized)(this);
  145. this[kPipeline].push({ $skip });
  146. return this;
  147. }
  148. /** Add a sort stage to the aggregation pipeline */
  149. sort($sort) {
  150. (0, abstract_cursor_1.assertUninitialized)(this);
  151. this[kPipeline].push({ $sort });
  152. return this;
  153. }
  154. /** Add a unwind stage to the aggregation pipeline */
  155. unwind($unwind) {
  156. (0, abstract_cursor_1.assertUninitialized)(this);
  157. this[kPipeline].push({ $unwind });
  158. return this;
  159. }
  160. /** Add a geoNear stage to the aggregation pipeline */
  161. geoNear($geoNear) {
  162. (0, abstract_cursor_1.assertUninitialized)(this);
  163. this[kPipeline].push({ $geoNear });
  164. return this;
  165. }
  166. }
  167. exports.AggregationCursor = AggregationCursor;
  168. //# sourceMappingURL=aggregation_cursor.js.map