aggregate Is the New find
The aggregate
command in MongoDB is similar to the find
command. You can provide the criteria for your query in the form of JSON documents, and it outputs a cursor
containing the search result. Sounds simple, right? That's because it is. Although aggregations can become very large and complex, at their core, they are relatively simple.
The key element in aggregation is called the pipeline. We will cover it in detail shortly, but at a high level, a pipeline is a series of instructions, where the input to each instruction is the output of the previous one. Simply put, aggregation is a method for taking a collection and, in a procedural way, filtering, transforming, and joining data from other collections to create new, meaningful datasets.
Aggregate Syntax
The aggregate
command operates on a collection like the other Create, Read, Update, Delete (CRUD) commands, like so:
use sample_mflix; var pipeline = [] // The pipeline is an array of stages...