UNPKG

35.5 kBJavaScriptView Raw
1'use strict';
2
3/*!
4 * Module dependencies
5 */
6
7const AggregationCursor = require('./cursor/aggregationCursor');
8const MongooseError = require('./error/mongooseError');
9const Query = require('./query');
10const { applyGlobalMaxTimeMS, applyGlobalDiskUse } = require('./helpers/query/applyGlobalOption');
11const clone = require('./helpers/clone');
12const getConstructorName = require('./helpers/getConstructorName');
13const prepareDiscriminatorPipeline = require('./helpers/aggregate/prepareDiscriminatorPipeline');
14const stringifyFunctionOperators = require('./helpers/aggregate/stringifyFunctionOperators');
15const utils = require('./utils');
16const read = Query.prototype.read;
17const readConcern = Query.prototype.readConcern;
18
19const validRedactStringValues = new Set(['$$DESCEND', '$$PRUNE', '$$KEEP']);
20
21/**
22 * Aggregate constructor used for building aggregation pipelines. Do not
23 * instantiate this class directly, use [Model.aggregate()](https://mongoosejs.com/docs/api/model.html#Model.aggregate()) instead.
24 *
25 * #### Example:
26 *
27 * const aggregate = Model.aggregate([
28 * { $project: { a: 1, b: 1 } },
29 * { $skip: 5 }
30 * ]);
31 *
32 * Model.
33 * aggregate([{ $match: { age: { $gte: 21 }}}]).
34 * unwind('tags').
35 * exec();
36 *
37 * #### Note:
38 *
39 * - The documents returned are plain javascript objects, not mongoose documents (since any shape of document can be returned).
40 * - Mongoose does **not** cast pipeline stages. The below will **not** work unless `_id` is a string in the database
41 *
42 * new Aggregate([{ $match: { _id: '00000000000000000000000a' } }]);
43 * // Do this instead to cast to an ObjectId
44 * new Aggregate([{ $match: { _id: new mongoose.Types.ObjectId('00000000000000000000000a') } }]);
45 *
46 * @see MongoDB https://www.mongodb.com/docs/manual/applications/aggregation/
47 * @see driver https://mongodb.github.io/node-mongodb-native/4.9/classes/Collection.html#aggregate
48 * @param {Array} [pipeline] aggregation pipeline as an array of objects
49 * @param {Model} [model] the model to use with this aggregate.
50 * @api public
51 */
52
53function Aggregate(pipeline, model) {
54 this._pipeline = [];
55 this._model = model;
56 this.options = {};
57
58 if (arguments.length === 1 && Array.isArray(pipeline)) {
59 this.append.apply(this, pipeline);
60 }
61}
62
63/**
64 * Contains options passed down to the [aggregate command](https://www.mongodb.com/docs/manual/reference/command/aggregate/).
65 * Supported options are:
66 *
67 * - [`allowDiskUse`](https://mongoosejs.com/docs/api/aggregate.html#Aggregate.prototype.allowDiskUse())
68 * - `bypassDocumentValidation`
69 * - [`collation`](https://mongoosejs.com/docs/api/aggregate.html#Aggregate.prototype.collation())
70 * - `comment`
71 * - [`cursor`](https://mongoosejs.com/docs/api/aggregate.html#Aggregate.prototype.cursor())
72 * - [`explain`](https://mongoosejs.com/docs/api/aggregate.html#Aggregate.prototype.explain())
73 * - `fieldsAsRaw`
74 * - [`hint`](https://mongoosejs.com/docs/api/aggregate.html#Aggregate.prototype.hint())
75 * - `let`
76 * - `maxTimeMS`
77 * - `raw`
78 * - [`readConcern`](https://mongoosejs.com/docs/api/aggregate.html#Aggregate.prototype.readConcern())
79 * - `readPreference`
80 * - [`session`](https://mongoosejs.com/docs/api/aggregate.html#Aggregate.prototype.session())
81 * - `writeConcern`
82 *
83 * @property options
84 * @memberOf Aggregate
85 * @api public
86 */
87
88Aggregate.prototype.options;
89
90/**
91 * Get/set the model that this aggregation will execute on.
92 *
93 * #### Example:
94 *
95 * const aggregate = MyModel.aggregate([{ $match: { answer: 42 } }]);
96 * aggregate.model() === MyModel; // true
97 *
98 * // Change the model. There's rarely any reason to do this.
99 * aggregate.model(SomeOtherModel);
100 * aggregate.model() === SomeOtherModel; // true
101 *
102 * @param {Model} [model] Set the model associated with this aggregate. If not provided, returns the already stored model.
103 * @return {Model}
104 * @api public
105 */
106
107Aggregate.prototype.model = function(model) {
108 if (arguments.length === 0) {
109 return this._model;
110 }
111
112 this._model = model;
113 if (model.schema != null) {
114 if (this.options.readPreference == null &&
115 model.schema.options.read != null) {
116 this.options.readPreference = model.schema.options.read;
117 }
118 if (this.options.collation == null &&
119 model.schema.options.collation != null) {
120 this.options.collation = model.schema.options.collation;
121 }
122 }
123
124 return model;
125};
126
127/**
128 * Appends new operators to this aggregate pipeline
129 *
130 * #### Example:
131 *
132 * aggregate.append({ $project: { field: 1 }}, { $limit: 2 });
133 *
134 * // or pass an array
135 * const pipeline = [{ $match: { daw: 'Logic Audio X' }} ];
136 * aggregate.append(pipeline);
137 *
138 * @param {...Object|Object[]} ops operator(s) to append. Can either be a spread of objects or a single parameter of a object array.
139 * @return {Aggregate}
140 * @api public
141 */
142
143Aggregate.prototype.append = function() {
144 const args = (arguments.length === 1 && Array.isArray(arguments[0]))
145 ? arguments[0]
146 : [...arguments];
147
148 if (!args.every(isOperator)) {
149 throw new Error('Arguments must be aggregate pipeline operators');
150 }
151
152 this._pipeline = this._pipeline.concat(args);
153
154 return this;
155};
156
157/**
158 * Appends a new $addFields operator to this aggregate pipeline.
159 * Requires MongoDB v3.4+ to work
160 *
161 * #### Example:
162 *
163 * // adding new fields based on existing fields
164 * aggregate.addFields({
165 * newField: '$b.nested'
166 * , plusTen: { $add: ['$val', 10]}
167 * , sub: {
168 * name: '$a'
169 * }
170 * })
171 *
172 * // etc
173 * aggregate.addFields({ salary_k: { $divide: [ "$salary", 1000 ] } });
174 *
175 * @param {Object} arg field specification
176 * @see $addFields https://www.mongodb.com/docs/manual/reference/operator/aggregation/addFields/
177 * @return {Aggregate}
178 * @api public
179 */
180Aggregate.prototype.addFields = function(arg) {
181 if (typeof arg !== 'object' || arg === null || Array.isArray(arg)) {
182 throw new Error('Invalid addFields() argument. Must be an object');
183 }
184 return this.append({ $addFields: Object.assign({}, arg) });
185};
186
187/**
188 * Appends a new $project operator to this aggregate pipeline.
189 *
190 * Mongoose query [selection syntax](https://mongoosejs.com/docs/api/query.html#Query.prototype.select()) is also supported.
191 *
192 * #### Example:
193 *
194 * // include a, include b, exclude _id
195 * aggregate.project("a b -_id");
196 *
197 * // or you may use object notation, useful when
198 * // you have keys already prefixed with a "-"
199 * aggregate.project({a: 1, b: 1, _id: 0});
200 *
201 * // reshaping documents
202 * aggregate.project({
203 * newField: '$b.nested'
204 * , plusTen: { $add: ['$val', 10]}
205 * , sub: {
206 * name: '$a'
207 * }
208 * })
209 *
210 * // etc
211 * aggregate.project({ salary_k: { $divide: [ "$salary", 1000 ] } });
212 *
213 * @param {Object|String} arg field specification
214 * @see projection https://www.mongodb.com/docs/manual/reference/aggregation/project/
215 * @return {Aggregate}
216 * @api public
217 */
218
219Aggregate.prototype.project = function(arg) {
220 const fields = {};
221
222 if (typeof arg === 'object' && !Array.isArray(arg)) {
223 Object.keys(arg).forEach(function(field) {
224 fields[field] = arg[field];
225 });
226 } else if (arguments.length === 1 && typeof arg === 'string') {
227 arg.split(/\s+/).forEach(function(field) {
228 if (!field) {
229 return;
230 }
231 const include = field[0] === '-' ? 0 : 1;
232 if (include === 0) {
233 field = field.substring(1);
234 }
235 fields[field] = include;
236 });
237 } else {
238 throw new Error('Invalid project() argument. Must be string or object');
239 }
240
241 return this.append({ $project: fields });
242};
243
244/**
245 * Appends a new custom $group operator to this aggregate pipeline.
246 *
247 * #### Example:
248 *
249 * aggregate.group({ _id: "$department" });
250 *
251 * @see $group https://www.mongodb.com/docs/manual/reference/aggregation/group/
252 * @method group
253 * @memberOf Aggregate
254 * @instance
255 * @param {Object} arg $group operator contents
256 * @return {Aggregate}
257 * @api public
258 */
259
260/**
261 * Appends a new custom $match operator to this aggregate pipeline.
262 *
263 * #### Example:
264 *
265 * aggregate.match({ department: { $in: [ "sales", "engineering" ] } });
266 *
267 * @see $match https://www.mongodb.com/docs/manual/reference/aggregation/match/
268 * @method match
269 * @memberOf Aggregate
270 * @instance
271 * @param {Object} arg $match operator contents
272 * @return {Aggregate}
273 * @api public
274 */
275
276/**
277 * Appends a new $skip operator to this aggregate pipeline.
278 *
279 * #### Example:
280 *
281 * aggregate.skip(10);
282 *
283 * @see $skip https://www.mongodb.com/docs/manual/reference/aggregation/skip/
284 * @method skip
285 * @memberOf Aggregate
286 * @instance
287 * @param {Number} num number of records to skip before next stage
288 * @return {Aggregate}
289 * @api public
290 */
291
292/**
293 * Appends a new $limit operator to this aggregate pipeline.
294 *
295 * #### Example:
296 *
297 * aggregate.limit(10);
298 *
299 * @see $limit https://www.mongodb.com/docs/manual/reference/aggregation/limit/
300 * @method limit
301 * @memberOf Aggregate
302 * @instance
303 * @param {Number} num maximum number of records to pass to the next stage
304 * @return {Aggregate}
305 * @api public
306 */
307
308
309/**
310 * Appends a new $densify operator to this aggregate pipeline.
311 *
312 * #### Example:
313 *
314 * aggregate.densify({
315 * field: 'timestamp',
316 * range: {
317 * step: 1,
318 * unit: 'hour',
319 * bounds: [new Date('2021-05-18T00:00:00.000Z'), new Date('2021-05-18T08:00:00.000Z')]
320 * }
321 * });
322 *
323 * @see $densify https://www.mongodb.com/docs/manual/reference/operator/aggregation/densify/
324 * @method densify
325 * @memberOf Aggregate
326 * @instance
327 * @param {Object} arg $densify operator contents
328 * @return {Aggregate}
329 * @api public
330 */
331
332/**
333 * Appends a new $fill operator to this aggregate pipeline.
334 *
335 * #### Example:
336 *
337 * aggregate.fill({
338 * output: {
339 * bootsSold: { value: 0 },
340 * sandalsSold: { value: 0 },
341 * sneakersSold: { value: 0 }
342 * }
343 * });
344 *
345 * @see $fill https://www.mongodb.com/docs/manual/reference/operator/aggregation/fill/
346 * @method fill
347 * @memberOf Aggregate
348 * @instance
349 * @param {Object} arg $fill operator contents
350 * @return {Aggregate}
351 * @api public
352 */
353
354/**
355 * Appends a new $geoNear operator to this aggregate pipeline.
356 *
357 * #### Note:
358 *
359 * **MUST** be used as the first operator in the pipeline.
360 *
361 * #### Example:
362 *
363 * aggregate.near({
364 * near: { type: 'Point', coordinates: [40.724, -73.997] },
365 * distanceField: "dist.calculated", // required
366 * maxDistance: 0.008,
367 * query: { type: "public" },
368 * includeLocs: "dist.location",
369 * spherical: true,
370 * });
371 *
372 * @see $geoNear https://www.mongodb.com/docs/manual/reference/aggregation/geoNear/
373 * @method near
374 * @memberOf Aggregate
375 * @instance
376 * @param {Object} arg
377 * @return {Aggregate}
378 * @api public
379 */
380
381Aggregate.prototype.near = function(arg) {
382 const op = {};
383 op.$geoNear = arg;
384 return this.append(op);
385};
386
387/*!
388 * define methods
389 */
390
391'group match skip limit out densify fill'.split(' ').forEach(function($operator) {
392 Aggregate.prototype[$operator] = function(arg) {
393 const op = {};
394 op['$' + $operator] = arg;
395 return this.append(op);
396 };
397});
398
399/**
400 * Appends new custom $unwind operator(s) to this aggregate pipeline.
401 *
402 * Note that the `$unwind` operator requires the path name to start with '$'.
403 * Mongoose will prepend '$' if the specified field doesn't start '$'.
404 *
405 * #### Example:
406 *
407 * aggregate.unwind("tags");
408 * aggregate.unwind("a", "b", "c");
409 * aggregate.unwind({ path: '$tags', preserveNullAndEmptyArrays: true });
410 *
411 * @see $unwind https://www.mongodb.com/docs/manual/reference/aggregation/unwind/
412 * @param {String|Object|String[]|Object[]} fields the field(s) to unwind, either as field names or as [objects with options](https://www.mongodb.com/docs/manual/reference/operator/aggregation/unwind/#document-operand-with-options). If passing a string, prefixing the field name with '$' is optional. If passing an object, `path` must start with '$'.
413 * @return {Aggregate}
414 * @api public
415 */
416
417Aggregate.prototype.unwind = function() {
418 const args = [...arguments];
419
420 const res = [];
421 for (const arg of args) {
422 if (arg && typeof arg === 'object') {
423 res.push({ $unwind: arg });
424 } else if (typeof arg === 'string') {
425 res.push({
426 $unwind: (arg[0] === '$') ? arg : '$' + arg
427 });
428 } else {
429 throw new Error('Invalid arg "' + arg + '" to unwind(), ' +
430 'must be string or object');
431 }
432 }
433
434 return this.append.apply(this, res);
435};
436
437/**
438 * Appends a new $replaceRoot operator to this aggregate pipeline.
439 *
440 * Note that the `$replaceRoot` operator requires field strings to start with '$'.
441 * If you are passing in a string Mongoose will prepend '$' if the specified field doesn't start '$'.
442 * If you are passing in an object the strings in your expression will not be altered.
443 *
444 * #### Example:
445 *
446 * aggregate.replaceRoot("user");
447 *
448 * aggregate.replaceRoot({ x: { $concat: ['$this', '$that'] } });
449 *
450 * @see $replaceRoot https://www.mongodb.com/docs/manual/reference/operator/aggregation/replaceRoot
451 * @param {String|Object} newRoot the field or document which will become the new root document
452 * @return {Aggregate}
453 * @api public
454 */
455
456Aggregate.prototype.replaceRoot = function(newRoot) {
457 let ret;
458
459 if (typeof newRoot === 'string') {
460 ret = newRoot.startsWith('$') ? newRoot : '$' + newRoot;
461 } else {
462 ret = newRoot;
463 }
464
465 return this.append({
466 $replaceRoot: {
467 newRoot: ret
468 }
469 });
470};
471
472/**
473 * Appends a new $count operator to this aggregate pipeline.
474 *
475 * #### Example:
476 *
477 * aggregate.count("userCount");
478 *
479 * @see $count https://www.mongodb.com/docs/manual/reference/operator/aggregation/count
480 * @param {String} fieldName The name of the output field which has the count as its value. It must be a non-empty string, must not start with $ and must not contain the . character.
481 * @return {Aggregate}
482 * @api public
483 */
484
485Aggregate.prototype.count = function(fieldName) {
486 return this.append({ $count: fieldName });
487};
488
489/**
490 * Appends a new $sortByCount operator to this aggregate pipeline. Accepts either a string field name
491 * or a pipeline object.
492 *
493 * Note that the `$sortByCount` operator requires the new root to start with '$'.
494 * Mongoose will prepend '$' if the specified field name doesn't start with '$'.
495 *
496 * #### Example:
497 *
498 * aggregate.sortByCount('users');
499 * aggregate.sortByCount({ $mergeObjects: [ "$employee", "$business" ] })
500 *
501 * @see $sortByCount https://www.mongodb.com/docs/manual/reference/operator/aggregation/sortByCount/
502 * @param {Object|String} arg
503 * @return {Aggregate} this
504 * @api public
505 */
506
507Aggregate.prototype.sortByCount = function(arg) {
508 if (arg && typeof arg === 'object') {
509 return this.append({ $sortByCount: arg });
510 } else if (typeof arg === 'string') {
511 return this.append({
512 $sortByCount: (arg[0] === '$') ? arg : '$' + arg
513 });
514 } else {
515 throw new TypeError('Invalid arg "' + arg + '" to sortByCount(), ' +
516 'must be string or object');
517 }
518};
519
520/**
521 * Appends new custom $lookup operator to this aggregate pipeline.
522 *
523 * #### Example:
524 *
525 * aggregate.lookup({ from: 'users', localField: 'userId', foreignField: '_id', as: 'users' });
526 *
527 * @see $lookup https://www.mongodb.com/docs/manual/reference/operator/aggregation/lookup/#pipe._S_lookup
528 * @param {Object} options to $lookup as described in the above link
529 * @return {Aggregate}
530 * @api public
531 */
532
533Aggregate.prototype.lookup = function(options) {
534 return this.append({ $lookup: options });
535};
536
537/**
538 * Appends new custom $graphLookup operator(s) to this aggregate pipeline, performing a recursive search on a collection.
539 *
540 * Note that graphLookup can only consume at most 100MB of memory, and does not allow disk use even if `{ allowDiskUse: true }` is specified.
541 *
542 * #### Example:
543 *
544 * // Suppose we have a collection of courses, where a document might look like `{ _id: 0, name: 'Calculus', prerequisite: 'Trigonometry'}` and `{ _id: 0, name: 'Trigonometry', prerequisite: 'Algebra' }`
545 * aggregate.graphLookup({ from: 'courses', startWith: '$prerequisite', connectFromField: 'prerequisite', connectToField: 'name', as: 'prerequisites', maxDepth: 3 }) // this will recursively search the 'courses' collection up to 3 prerequisites
546 *
547 * @see $graphLookup https://www.mongodb.com/docs/manual/reference/operator/aggregation/graphLookup/#pipe._S_graphLookup
548 * @param {Object} options to $graphLookup as described in the above link
549 * @return {Aggregate}
550 * @api public
551 */
552
553Aggregate.prototype.graphLookup = function(options) {
554 const cloneOptions = {};
555 if (options) {
556 if (!utils.isObject(options)) {
557 throw new TypeError('Invalid graphLookup() argument. Must be an object.');
558 }
559
560 utils.mergeClone(cloneOptions, options);
561 const startWith = cloneOptions.startWith;
562
563 if (startWith && typeof startWith === 'string') {
564 cloneOptions.startWith = cloneOptions.startWith.startsWith('$') ?
565 cloneOptions.startWith :
566 '$' + cloneOptions.startWith;
567 }
568
569 }
570 return this.append({ $graphLookup: cloneOptions });
571};
572
573/**
574 * Appends new custom $sample operator to this aggregate pipeline.
575 *
576 * #### Example:
577 *
578 * aggregate.sample(3); // Add a pipeline that picks 3 random documents
579 *
580 * @see $sample https://www.mongodb.com/docs/manual/reference/operator/aggregation/sample/#pipe._S_sample
581 * @param {Number} size number of random documents to pick
582 * @return {Aggregate}
583 * @api public
584 */
585
586Aggregate.prototype.sample = function(size) {
587 return this.append({ $sample: { size: size } });
588};
589
590/**
591 * Appends a new $sort operator to this aggregate pipeline.
592 *
593 * If an object is passed, values allowed are `asc`, `desc`, `ascending`, `descending`, `1`, and `-1`.
594 *
595 * If a string is passed, it must be a space delimited list of path names. The sort order of each path is ascending unless the path name is prefixed with `-` which will be treated as descending.
596 *
597 * #### Example:
598 *
599 * // these are equivalent
600 * aggregate.sort({ field: 'asc', test: -1 });
601 * aggregate.sort('field -test');
602 *
603 * @see $sort https://www.mongodb.com/docs/manual/reference/aggregation/sort/
604 * @param {Object|String} arg
605 * @return {Aggregate} this
606 * @api public
607 */
608
609Aggregate.prototype.sort = function(arg) {
610 // TODO refactor to reuse the query builder logic
611
612 const sort = {};
613
614 if (getConstructorName(arg) === 'Object') {
615 const desc = ['desc', 'descending', -1];
616 Object.keys(arg).forEach(function(field) {
617 // If sorting by text score, skip coercing into 1/-1
618 if (arg[field] instanceof Object && arg[field].$meta) {
619 sort[field] = arg[field];
620 return;
621 }
622 sort[field] = desc.indexOf(arg[field]) === -1 ? 1 : -1;
623 });
624 } else if (arguments.length === 1 && typeof arg === 'string') {
625 arg.split(/\s+/).forEach(function(field) {
626 if (!field) {
627 return;
628 }
629 const ascend = field[0] === '-' ? -1 : 1;
630 if (ascend === -1) {
631 field = field.substring(1);
632 }
633 sort[field] = ascend;
634 });
635 } else {
636 throw new TypeError('Invalid sort() argument. Must be a string or object.');
637 }
638
639 return this.append({ $sort: sort });
640};
641
642/**
643 * Appends new $unionWith operator to this aggregate pipeline.
644 *
645 * #### Example:
646 *
647 * aggregate.unionWith({ coll: 'users', pipeline: [ { $match: { _id: 1 } } ] });
648 *
649 * @see $unionWith https://www.mongodb.com/docs/manual/reference/operator/aggregation/unionWith
650 * @param {Object} options to $unionWith query as described in the above link
651 * @return {Aggregate}
652 * @api public
653 */
654
655Aggregate.prototype.unionWith = function(options) {
656 return this.append({ $unionWith: options });
657};
658
659
660/**
661 * Sets the readPreference option for the aggregation query.
662 *
663 * #### Example:
664 *
665 * await Model.aggregate(pipeline).read('primaryPreferred');
666 *
667 * @param {String|ReadPreference} pref one of the listed preference options or their aliases
668 * @param {Array} [tags] optional tags for this query.
669 * @return {Aggregate} this
670 * @api public
671 * @see mongodb https://www.mongodb.com/docs/manual/applications/replication/#read-preference
672 */
673
674Aggregate.prototype.read = function(pref, tags) {
675 read.call(this, pref, tags);
676 return this;
677};
678
679/**
680 * Sets the readConcern level for the aggregation query.
681 *
682 * #### Example:
683 *
684 * await Model.aggregate(pipeline).readConcern('majority');
685 *
686 * @param {String} level one of the listed read concern level or their aliases
687 * @see mongodb https://www.mongodb.com/docs/manual/reference/read-concern/
688 * @return {Aggregate} this
689 * @api public
690 */
691
692Aggregate.prototype.readConcern = function(level) {
693 readConcern.call(this, level);
694 return this;
695};
696
697/**
698 * Appends a new $redact operator to this aggregate pipeline.
699 *
700 * If 3 arguments are supplied, Mongoose will wrap them with if-then-else of $cond operator respectively
701 * If `thenExpr` or `elseExpr` is string, make sure it starts with $$, like `$$DESCEND`, `$$PRUNE` or `$$KEEP`.
702 *
703 * #### Example:
704 *
705 * await Model.aggregate(pipeline).redact({
706 * $cond: {
707 * if: { $eq: [ '$level', 5 ] },
708 * then: '$$PRUNE',
709 * else: '$$DESCEND'
710 * }
711 * });
712 *
713 * // $redact often comes with $cond operator, you can also use the following syntax provided by mongoose
714 * await Model.aggregate(pipeline).redact({ $eq: [ '$level', 5 ] }, '$$PRUNE', '$$DESCEND');
715 *
716 * @param {Object} expression redact options or conditional expression
717 * @param {String|Object} [thenExpr] true case for the condition
718 * @param {String|Object} [elseExpr] false case for the condition
719 * @return {Aggregate} this
720 * @see $redact https://www.mongodb.com/docs/manual/reference/operator/aggregation/redact/
721 * @api public
722 */
723
724Aggregate.prototype.redact = function(expression, thenExpr, elseExpr) {
725 if (arguments.length === 3) {
726 if ((typeof thenExpr === 'string' && !validRedactStringValues.has(thenExpr)) ||
727 (typeof elseExpr === 'string' && !validRedactStringValues.has(elseExpr))) {
728 throw new Error('If thenExpr or elseExpr is string, it must be either $$DESCEND, $$PRUNE or $$KEEP');
729 }
730
731 expression = {
732 $cond: {
733 if: expression,
734 then: thenExpr,
735 else: elseExpr
736 }
737 };
738 } else if (arguments.length !== 1) {
739 throw new TypeError('Invalid arguments');
740 }
741
742 return this.append({ $redact: expression });
743};
744
745/**
746 * Execute the aggregation with explain
747 *
748 * #### Example:
749 *
750 * Model.aggregate(..).explain()
751 *
752 * @param {String} [verbosity]
753 * @return {Promise}
754 */
755
756Aggregate.prototype.explain = async function explain(verbosity) {
757 if (typeof verbosity === 'function' || typeof arguments[1] === 'function') {
758 throw new MongooseError('Aggregate.prototype.explain() no longer accepts a callback');
759 }
760 const model = this._model;
761
762 if (!this._pipeline.length) {
763 throw new Error('Aggregate has empty pipeline');
764 }
765
766 prepareDiscriminatorPipeline(this._pipeline, this._model.schema);
767
768 await new Promise((resolve, reject) => {
769 model.hooks.execPre('aggregate', this, error => {
770 if (error) {
771 const _opts = { error: error };
772 return model.hooks.execPost('aggregate', this, [null], _opts, error => {
773 reject(error);
774 });
775 } else {
776 resolve();
777 }
778 });
779 });
780
781 const cursor = model.collection.aggregate(this._pipeline, this.options);
782
783 if (verbosity == null) {
784 verbosity = true;
785 }
786
787 let result = null;
788 try {
789 result = await cursor.explain(verbosity);
790 } catch (error) {
791 await new Promise((resolve, reject) => {
792 const _opts = { error: error };
793 model.hooks.execPost('aggregate', this, [null], _opts, error => {
794 if (error) {
795 return reject(error);
796 }
797 return resolve();
798 });
799 });
800 }
801
802 const _opts = { error: null };
803 await new Promise((resolve, reject) => {
804 model.hooks.execPost('aggregate', this, [result], _opts, error => {
805 if (error) {
806 return reject(error);
807 }
808 return resolve();
809 });
810 });
811
812 return result;
813};
814
815/**
816 * Sets the allowDiskUse option for the aggregation query
817 *
818 * #### Example:
819 *
820 * await Model.aggregate([{ $match: { foo: 'bar' } }]).allowDiskUse(true);
821 *
822 * @param {Boolean} value Should tell server it can use hard drive to store data during aggregation.
823 * @return {Aggregate} this
824 * @see mongodb https://www.mongodb.com/docs/manual/reference/command/aggregate/
825 */
826
827Aggregate.prototype.allowDiskUse = function(value) {
828 this.options.allowDiskUse = value;
829 return this;
830};
831
832/**
833 * Sets the hint option for the aggregation query
834 *
835 * #### Example:
836 *
837 * Model.aggregate(..).hint({ qty: 1, category: 1 }).exec();
838 *
839 * @param {Object|String} value a hint object or the index name
840 * @return {Aggregate} this
841 * @see mongodb https://www.mongodb.com/docs/manual/reference/command/aggregate/
842 */
843
844Aggregate.prototype.hint = function(value) {
845 this.options.hint = value;
846 return this;
847};
848
849/**
850 * Sets the session for this aggregation. Useful for [transactions](https://mongoosejs.com/docs/transactions.html).
851 *
852 * #### Example:
853 *
854 * const session = await Model.startSession();
855 * await Model.aggregate(..).session(session);
856 *
857 * @param {ClientSession} session
858 * @return {Aggregate} this
859 * @see mongodb https://www.mongodb.com/docs/manual/reference/command/aggregate/
860 */
861
862Aggregate.prototype.session = function(session) {
863 if (session == null) {
864 delete this.options.session;
865 } else {
866 this.options.session = session;
867 }
868 return this;
869};
870
871/**
872 * Lets you set arbitrary options, for middleware or plugins.
873 *
874 * #### Example:
875 *
876 * const agg = Model.aggregate(..).option({ allowDiskUse: true }); // Set the `allowDiskUse` option
877 * agg.options; // `{ allowDiskUse: true }`
878 *
879 * @param {Object} options keys to merge into current options
880 * @param {Number} [options.maxTimeMS] number limits the time this aggregation will run, see [MongoDB docs on `maxTimeMS`](https://www.mongodb.com/docs/manual/reference/operator/meta/maxTimeMS/)
881 * @param {Boolean} [options.allowDiskUse] boolean if true, the MongoDB server will use the hard drive to store data during this aggregation
882 * @param {Object} [options.collation] object see [`Aggregate.prototype.collation()`](https://mongoosejs.com/docs/api/aggregate.html#Aggregate.prototype.collation())
883 * @param {ClientSession} [options.session] ClientSession see [`Aggregate.prototype.session()`](https://mongoosejs.com/docs/api/aggregate.html#Aggregate.prototype.session())
884 * @see mongodb https://www.mongodb.com/docs/manual/reference/command/aggregate/
885 * @return {Aggregate} this
886 * @api public
887 */
888
889Aggregate.prototype.option = function(value) {
890 for (const key in value) {
891 this.options[key] = value[key];
892 }
893 return this;
894};
895
896/**
897 * Sets the `cursor` option and executes this aggregation, returning an aggregation cursor.
898 * Cursors are useful if you want to process the results of the aggregation one-at-a-time
899 * because the aggregation result is too big to fit into memory.
900 *
901 * #### Example:
902 *
903 * const cursor = Model.aggregate(..).cursor({ batchSize: 1000 });
904 * cursor.eachAsync(function(doc, i) {
905 * // use doc
906 * });
907 *
908 * @param {Object} options
909 * @param {Number} [options.batchSize] set the cursor batch size
910 * @param {Boolean} [options.useMongooseAggCursor] use experimental mongoose-specific aggregation cursor (for `eachAsync()` and other query cursor semantics)
911 * @return {AggregationCursor} cursor representing this aggregation
912 * @api public
913 * @see mongodb https://mongodb.github.io/node-mongodb-native/4.9/classes/AggregationCursor.html
914 */
915
916Aggregate.prototype.cursor = function(options) {
917 this.options.cursor = options || {};
918 return new AggregationCursor(this); // return this;
919};
920
921/**
922 * Adds a collation
923 *
924 * #### Example:
925 *
926 * const res = await Model.aggregate(pipeline).collation({ locale: 'en_US', strength: 1 });
927 *
928 * @param {Object} collation options
929 * @return {Aggregate} this
930 * @api public
931 * @see mongodb https://mongodb.github.io/node-mongodb-native/4.9/interfaces/CollationOptions.html
932 */
933
934Aggregate.prototype.collation = function(collation) {
935 this.options.collation = collation;
936 return this;
937};
938
939/**
940 * Combines multiple aggregation pipelines.
941 *
942 * #### Example:
943 *
944 * const res = await Model.aggregate().facet({
945 * books: [{ groupBy: '$author' }],
946 * price: [{ $bucketAuto: { groupBy: '$price', buckets: 2 } }]
947 * });
948 *
949 * // Output: { books: [...], price: [{...}, {...}] }
950 *
951 * @param {Object} facet options
952 * @return {Aggregate} this
953 * @see $facet https://www.mongodb.com/docs/manual/reference/operator/aggregation/facet/
954 * @api public
955 */
956
957Aggregate.prototype.facet = function(options) {
958 return this.append({ $facet: options });
959};
960
961/**
962 * Helper for [Atlas Text Search](https://www.mongodb.com/docs/atlas/atlas-search/tutorial/)'s
963 * `$search` stage.
964 *
965 * #### Example:
966 *
967 * const res = await Model.aggregate().
968 * search({
969 * text: {
970 * query: 'baseball',
971 * path: 'plot'
972 * }
973 * });
974 *
975 * // Output: [{ plot: '...', title: '...' }]
976 *
977 * @param {Object} $search options
978 * @return {Aggregate} this
979 * @see $search https://www.mongodb.com/docs/atlas/atlas-search/tutorial/
980 * @api public
981 */
982
983Aggregate.prototype.search = function(options) {
984 return this.append({ $search: options });
985};
986
987/**
988 * Returns the current pipeline
989 *
990 * #### Example:
991 *
992 * MyModel.aggregate().match({ test: 1 }).pipeline(); // [{ $match: { test: 1 } }]
993 *
994 * @return {Array} The current pipeline similar to the operation that will be executed
995 * @api public
996 */
997
998Aggregate.prototype.pipeline = function() {
999 return this._pipeline;
1000};
1001
1002/**
1003 * Executes the aggregate pipeline on the currently bound Model.
1004 *
1005 * #### Example:
1006 * const result = await aggregate.exec();
1007 *
1008 * @return {Promise}
1009 * @api public
1010 */
1011
1012Aggregate.prototype.exec = async function exec() {
1013 if (!this._model) {
1014 throw new Error('Aggregate not bound to any Model');
1015 }
1016 if (typeof arguments[0] === 'function') {
1017 throw new MongooseError('Aggregate.prototype.exec() no longer accepts a callback');
1018 }
1019 const model = this._model;
1020 const collection = this._model.collection;
1021
1022 applyGlobalMaxTimeMS(this.options, model.db.options, model.base.options);
1023 applyGlobalDiskUse(this.options, model.db.options, model.base.options);
1024
1025 const asyncLocalStorage = this.model()?.db?.base.transactionAsyncLocalStorage?.getStore();
1026 if (!this.options.hasOwnProperty('session') && asyncLocalStorage?.session != null) {
1027 this.options.session = asyncLocalStorage.session;
1028 }
1029
1030 if (this.options && this.options.cursor) {
1031 return new AggregationCursor(this);
1032 }
1033
1034 prepareDiscriminatorPipeline(this._pipeline, this._model.schema);
1035 stringifyFunctionOperators(this._pipeline);
1036
1037 await new Promise((resolve, reject) => {
1038 model.hooks.execPre('aggregate', this, error => {
1039 if (error) {
1040 const _opts = { error: error };
1041 return model.hooks.execPost('aggregate', this, [null], _opts, error => {
1042 reject(error);
1043 });
1044 } else {
1045 resolve();
1046 }
1047 });
1048 });
1049
1050 if (!this._pipeline.length) {
1051 throw new MongooseError('Aggregate has empty pipeline');
1052 }
1053
1054 const options = clone(this.options || {});
1055 let result;
1056 try {
1057 const cursor = await collection.aggregate(this._pipeline, options);
1058 result = await cursor.toArray();
1059 } catch (error) {
1060 await new Promise((resolve, reject) => {
1061 const _opts = { error: error };
1062 model.hooks.execPost('aggregate', this, [null], _opts, (error) => {
1063 if (error) {
1064 return reject(error);
1065 }
1066
1067 resolve();
1068 });
1069 });
1070 }
1071
1072 const _opts = { error: null };
1073 await new Promise((resolve, reject) => {
1074 model.hooks.execPost('aggregate', this, [result], _opts, error => {
1075 if (error) {
1076 return reject(error);
1077 }
1078 return resolve();
1079 });
1080 });
1081
1082 return result;
1083};
1084
1085/**
1086 * Provides a Promise-like `then` function, which will call `.exec` without a callback
1087 * Compatible with `await`.
1088 *
1089 * #### Example:
1090 *
1091 * Model.aggregate(..).then(successCallback, errorCallback);
1092 *
1093 * @param {Function} [resolve] successCallback
1094 * @param {Function} [reject] errorCallback
1095 * @return {Promise}
1096 */
1097Aggregate.prototype.then = function(resolve, reject) {
1098 return this.exec().then(resolve, reject);
1099};
1100
1101/**
1102 * Executes the aggregation returning a `Promise` which will be
1103 * resolved with either the doc(s) or rejected with the error.
1104 * Like [`.then()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.then), but only takes a rejection handler.
1105 * Compatible with `await`.
1106 *
1107 * @param {Function} [reject]
1108 * @return {Promise}
1109 * @api public
1110 */
1111
1112Aggregate.prototype.catch = function(reject) {
1113 return this.exec().then(null, reject);
1114};
1115
1116/**
1117 * Executes the aggregate returning a `Promise` which will be
1118 * resolved with `.finally()` chained.
1119 *
1120 * More about [Promise `finally()` in JavaScript](https://thecodebarbarian.com/using-promise-finally-in-node-js.html).
1121 *
1122 * @param {Function} [onFinally]
1123 * @return {Promise}
1124 * @api public
1125 */
1126
1127Aggregate.prototype.finally = function(onFinally) {
1128 return this.exec().finally(onFinally);
1129};
1130
1131/**
1132 * Returns an asyncIterator for use with [`for/await/of` loops](https://thecodebarbarian.com/getting-started-with-async-iterators-in-node-js)
1133 * You do not need to call this function explicitly, the JavaScript runtime
1134 * will call it for you.
1135 *
1136 * #### Example:
1137 *
1138 * const agg = Model.aggregate([{ $match: { age: { $gte: 25 } } }]);
1139 * for await (const doc of agg) {
1140 * console.log(doc.name);
1141 * }
1142 *
1143 * Node.js 10.x supports async iterators natively without any flags. You can
1144 * enable async iterators in Node.js 8.x using the [`--harmony_async_iteration` flag](https://github.com/tc39/proposal-async-iteration/issues/117#issuecomment-346695187).
1145 *
1146 * **Note:** This function is not set if `Symbol.asyncIterator` is undefined. If
1147 * `Symbol.asyncIterator` is undefined, that means your Node.js version does not
1148 * support async iterators.
1149 *
1150 * @method [Symbol.asyncIterator]
1151 * @memberOf Aggregate
1152 * @instance
1153 * @api public
1154 */
1155
1156if (Symbol.asyncIterator != null) {
1157 Aggregate.prototype[Symbol.asyncIterator] = function() {
1158 return this.cursor({ useMongooseAggCursor: true }).transformNull()._transformForAsyncIterator();
1159 };
1160}
1161
1162/*!
1163 * Helpers
1164 */
1165
1166/**
1167 * Checks whether an object is likely a pipeline operator
1168 *
1169 * @param {Object} obj object to check
1170 * @return {Boolean}
1171 * @api private
1172 */
1173
1174function isOperator(obj) {
1175 if (typeof obj !== 'object' || obj === null) {
1176 return false;
1177 }
1178
1179 const k = Object.keys(obj);
1180
1181 return k.length === 1 && k[0][0] === '$';
1182}
1183
1184/**
1185 * Adds the appropriate `$match` pipeline step to the top of an aggregate's
1186 * pipeline, should it's model is a non-root discriminator type. This is
1187 * analogous to the `prepareDiscriminatorCriteria` function in `lib/query.js`.
1188 *
1189 * @param {Aggregate} aggregate Aggregate to prepare
1190 * @api private
1191 */
1192
1193Aggregate._prepareDiscriminatorPipeline = prepareDiscriminatorPipeline;
1194
1195/*!
1196 * Exports
1197 */
1198
1199module.exports = Aggregate;