aggregate.js 32 KB

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