I am using aggregate on mongodb.
This aggregate gets the number of documents that are matched to “user.isLogin : true”.
db.getCollection('example').aggregate([
{
$lookup: {
from: "users",
localField: "userId",
foreignField: "userId",
as: "user"
}
},
{$match: {user.isLogin : true}}
{ $count: "total_count" }
])
This aggregate takes more than 4 seconds now.
When I remove the $count, it takes 0.173 secs.
How can I speed up to 0.173 secs when I am using the $count?
I know $count is same to $group:{_id: null, total_count:{$sum: 1}} so this question is how to speed up the group stage after lookup stage?
Thanks, all.