Facing Slow Performance for Filtered Count Queries

When i run db.samplecollection.aggregate(
[
{
$group: {
_id: “$user_id”,
total: {
$sum: 1
}
}
}]) this in 3million documents, even if i created index on user id… it is taking some time… And Is any best alternative way for getting count in group or after filter some data in query stage

Hi @Prince_N_A3,

I did some research on this, checking how long it should realistically take and it looks like operations like this with this much data usually run for a minute max, which is the expected behaviour.

I think what you can do instead is to add this new option explain whenever you run this command.

The explain option wouldn’t make things faster but should give you a clearer idea of why it’s taking the time it does.

Add it like this:

db.samplecollection.aggregate([
  { $group: { _id: "$user_id", total: { $sum: 1 } } }
], { explain: true })

Here’s a link that explains what the explain option does -

Hope this helps!