I am trying to group by and count with mongodb. Whatever I tried I couldn't do it. This is my query
db.getCollection("requests").find(
{
"updatedAt" : {
"$gt" : ISODate("2020-05-14")
},
"status" : "SUGGESTED"
},
{
"user.email" : 1.0,
"user.name" : 1.0,
"updatedBy.displayName" : 1.0,
"requestTerm" : 1.0
}
)
I would like to group and count based on updatedBy.displayName
So result will be for me distinct displayname ( Column1), count ( number of times )
How can I achieve this?
This is what I tried and it seems it doesn't work
db.requests.aggregate(
{$match: { "updatedAt" : {
"$gt" : ISODate("2020-05-14")
},
"status" : "SUGGESTED" }},
{$group: {updatedBy: "$updatedBy.displayName", number: {$sum: 1}}}
)
asked May 14, 2020 at 21:20
1 Answer 1
According to the mongo manual on $group, the command should be like this.
db.requests.aggregate(
[
{$match: {
"updatedAt" : {"$gt" : ISODate("2020-05-14")},
"status" : "SUGGESTED"
}},
{$group: {
_id: {updatedBy: "$updatedBy.displayName"},
number: {$sum: 1}
}}
]
)
lang-sql