Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

aggregate.js 32KB

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