0

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

0

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}
 }}
 ]
)
answered May 15, 2020 at 8:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.