Aggregate()

Parameters
  • [pipeline] «Array» aggregation pipeline as an array of objects

Aggregate constructor used for building aggregation pipelines. Do not instantiate this class directly, use Model.aggregate() instead.

Example:

  1. const aggregate = Model.aggregate([
  2. { $project: { a: 1, b: 1 } },
  3. { $skip: 5 }
  4. ]);
  5. Model.
  6. aggregate([{ $match: { age: { $gte: 21 }}}]).
  7. unwind('tags').
  8. exec(callback);

Note:

  • The documents returned are plain javascript objects, not mongoose documents (since any shape of document can be returned).
  • Mongoose does not cast pipeline stages. The below will not work unless _id is a string in the database
  1. new Aggregate([{ $match: { _id: '00000000000000000000000a' } }]);
  2. // Do this instead to cast to an ObjectId
  3. new Aggregate([{ $match: { _id: mongoose.Types.ObjectId('00000000000000000000000a') } }]);