aggregate.js 35 KB

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