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 31KB

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