MongoDB Aggregation

MongoDB Aggregation

transform and combine data from multiple documents to generate new information not available in any single document.

you can think of the aggregation framework as MongoDB’s equivalent to the SQL GROUP BY clause.

Screen Shot 2020-11-05 at 11.29.58 PM

PipeLine

Screen Shot 2020-11-05 at 11.28.14 PM
  • $project —Specify fields to be placed in the output document (projected).
  • $match —Select documents to be processed, similar to find().
  • $limit —Limit the number of documents to be passed to the next step.
  • $skip —Skip a specified number of documents.
  • $unwind —Expand an array, generating one output document for each array entry.
  • $group —Group documents by a specified key.
  • $sort —Sort documents.
  • $geoNear —Select documents near a geospatial location.
  • $out —Write the results of the pipeline to a collection (new in v2.6).
  • $redact —Control access to certain data (new in v2.6).
Screen Shot 2020-11-05 at 11.29.30 PM

pseudo-join

Screen Shot 2020-11-06 at 1.24.25 PM

out + project

With the $out operator, you can automatically save the output from a pipeline into a collection

The $project operator allows you to filter which fields will be passed to the next stage of the pipeline.

unwind

This operator allows you to expand an array, generating one output document for every input document array entry.

Screen Shot 2020-11-06 at 1.34.59 PM