How to get total document count in aggregation pipeline?

Raj
Raj
Member
54 Points
17 Posts

I'm trying following query:

db.getCollection('permission').aggregate([
   { $match: { "accountId": "my-account" } },
   { $group: { _id: "$accountId", "totalCount": { $sum: 1 } } }
])

It's showing total account wise but I want total count of all document in permission.

Views: 263
Total Answered: 1
Total Marked As Answer: 1
Posted On: 03-Jun-2022 02:51

Share:   fb twitter linkedin
Answers
Smith
Smith
Moderator
738 Points
154 Posts
         

Try to put null in _id as:

db.getCollection('permission').aggregate([
   { $group: { _id: null, "totalCount": { $sum: 1 } } }
])
Posted On: 12-Jun-2022 22:23
thanks
 - Raj  01-Aug-2022 23:08
 Log In to Chat