Database Reference
In-Depth Information
},
"count" : 18
},
{
"_id" : {
"color" : "orange",
"transport" : "autombile"
},
"count" : 18
},
{
"_id" : {
"color" : "green",
"transport" : "train"
},
"count" : 18
},
{
"_id" : {
"color" : "purple",
"transport" : "train"
},
"count" : 18
},
{
"_id" : {
"color" : "grey",
"transport" : "plane"
},
"count" : 18
}
],
"ok" : 1
}
You can now see the extra fields from the transport element added to _id , and we have limited the results to only
five. You should now see how we can build pipelines from multiple operators to draw data aggregated information
from our collection.
$match
The next operator we will review is $match , which is used to effectively return the results of a normal MongoDB query
within your aggregation pipeline. The $match operator is best used at the start of the pipeline to limit the number of
documents that are initially put into the pipeline; by limiting the number of documents processed, we significantly reduce
performance overhead. For example, suppose we want to perform our pipeline operations on only those documents that
have a num value greater than 500. We can use the query { num : { $gt : 500 } } to return all documents matching this
criterion. If we add this query as a $match to our to our existing aggregation, we get the following:
[
{ $match : { num : { $gt : 500 } } },
{ $group : { _id : { color: "$color", transport: "$transport"} , count : { $sum : 1 } } },
 
Search WWH ::




Custom Search