Difference between revisions of "MongoDB Aggregate Pipeline Nested"
(Created page with "Main Page >> MongoDB >>MongoDB Workbook >> Aggregation Pipeline and nested data == Aggregation Pipeline == So far '''find()''' either re...") |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
== Aggregation Pipeline == | == Aggregation Pipeline == | ||
− | So far '''find()''' either returns all the elements of an array, if one element matches the search criteria, or '''$elematch''' returns the first one found only. The latter is fine if there is only one to be found, but not so good if several items in the array should match the search criteria. | + | So far '''find()''' either returns all the elements of an array, if one element matches the search criteria, or '''$elematch''' returns the first one found only. The latter is fine if there is only one to be found, but not so good if several items in the array should match the search criteria. The aggregation pipeline can help with this. |
− | |||
− | The aggregation pipeline | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
=== $filter === | === $filter === | ||
Line 39: | Line 13: | ||
$filter has the following syntax: | $filter has the following syntax: | ||
− | <pre style="color: | + | <pre style="color:purple"> |
{ $filter: { | { $filter: { | ||
input: <array>, /* expression for the array */ | input: <array>, /* expression for the array */ | ||
Line 84: | Line 58: | ||
== Next Step == | == Next Step == | ||
− | [[MongoDB_UpdateNested|Updating]] | + | [[MongoDB_UpdateNested|Updating]] a collection with nested data |
Latest revision as of 20:24, 13 November 2017
Main Page >> MongoDB >>MongoDB Workbook >> Aggregation Pipeline and nested data
Aggregation Pipeline
So far find() either returns all the elements of an array, if one element matches the search criteria, or $elematch returns the first one found only. The latter is fine if there is only one to be found, but not so good if several items in the array should match the search criteria. The aggregation pipeline can help with this.
$filter
In the aggregation pipeline, array has various operators and the one we are interested in is $filter
This returns a subset of the array with only the elements that match the filter condition.
$filter has the following syntax:
{ $filter: { input: <array>, /* expression for the array */ as: <string>, /* variable name for the element */ cond: <expression> /* filter condition */ } }
$filter is one of the stages of the pipeline and can not be used by itself. It is used with an aggregation framework operator, such as $project.
We can use the pipeline to gather elements of our employees array to get the employees matching the query criteria only, rather than one, or everyone. This example has only one stage:
db.deptCollection.aggregate([ { $project: { empSet: { $filter: { input: "$employees", as: "employee", cond: { $gte: [ "$$employee.sal", 2000 ] } } } } } ]).pretty()
Now the system should only retrieve the employees with salary > 2000.
Count
The power of the aggregation pipeline is to do processing on the data.
Lets count how many employees each department has:
db.deptCollection.aggregate({ "$project": { "deptno": 1, "Count": { "$size": { "$ifNull": [ "$employees", [] ] } } }})
The $ifNull operator is needed, since department 40 has no employees - you will get an error message if left out!
Next Step
Updating a collection with nested data