π¦Group
Group documents
IMongoCollection<T>.Aggregate()
.Group(doc => doc.<field>, <output>)var collection = database.GetCollection<User>(collectionName);
var singleFieldAggregate = collection.Aggregate()
.Group(u => u.Profession,
ac => new {
profession = ac.Key,
total = ac.Sum(u => 1)
});
var groupedProfessions =
await singleFieldAggregate.ToListAsync();
foreach (var group in groupedProfessions)
{
Utils.Log($"{group.profession}: {group.total}");
}db.users.aggregate([
{
"$group":{
"_id":"$profession",
"total":{
"$sum":1
}
}
}
])
----------------------------
// sample results
/* 1 */
{
"_id" : "Lawyer",
"total" : 44
},
/* 2 */
{
"_id" : "Social-Media Manager",
"total" : 35
},
/* 3 */
{
"_id" : "Financial Adviser",
"total" : 34
}Group by an embedded document's field
Match - Group - Project
Last updated