[
{
"$group": {
"_id": "$username",
"total": {"$sum": 1}
}
}
]
select count(*) as total, * from table group by username
1
namidairo 2021-12-10 08:59:04 +08:00
```
[ { $group: { _id: "$username", amount: {$sum: 1}, items: {$push: "$$ROOT"} } } ] ``` push: https://docs.mongodb.com/manual/reference/operator/aggregation/push/#mongodb-group-grp.-push $$ROOT: https://docs.mongodb.com/manual/reference/aggregation-variables/#mongodb-variable-variable.ROOT |