π¦Group
Group documents
The group stage groups documents by a specified _id expression and outputs a document for each group. One way to use the Match stage in a pipeline is to use the Aggregate
method in a IMongoCollection<T>
and chain the Group
method.
The sample groups User
documents by their profession field and outputs the total documents per group.
Group by an embedded document's field
You can group by any document's field you want. The sample groups the User
documents by the Address.State field to find the total documents per state sorted by total.
Match - Group - Project
The sample combines 4 stages to calculate the average monthly expenses per gender.
The $match stage filters documents based on the salary
The $group stage groups the filtered documents per gender and calculate the average monthly expenses
The $project stage formats the gender as a string
The last stage $sort, simply sorts the results per average monthly expenses
The LINQ query supports the Math.Ceiling
method and can produce an average of Int32
type.
Last updated