Details
-
Bug
-
Resolution: Duplicate
-
Major - P3
-
None
-
6.0.9
-
None
-
None
-
Server Triage
-
ALL
Description
collection.countDocuments is converted in an aggregate with $math + $group by _id.
db.getCollection("test").countDocuments({"type":"test","auditEnabled":{"$ne":true}}) |
becomes:
db.getCollection("test").aggregate( |
[
|
{
|
"$match" : { |
"type" : "test", |
"auditEnabled" : { |
"$ne" : true |
}
|
}
|
},
|
{
|
"$group" : { |
"_id" : NumberInt(1), |
"n" : { |
"$sum" : NumberInt(1) |
}
|
}
|
}
|
],
|
{
|
"allowDiskUse" : false |
}
|
);
|
Problem is that if documents in the collection are large, they are fully loaded in memory (apparently) to aggregate them.
Why don't you add a $project by _id after $match? This will greatly improve (10x in our case) performance. Something like:
db.getCollection("test").aggregate( |
[
|
{
|
"$match" : { |
"type" : "test", |
"auditEnabled" : { |
"$ne" : true |
}
|
}
|
},
|
{
|
"$project" : |
{
|
"_id" : 1 |
}
|
},
|
{
|
"$group" : { |
"_id" : NumberInt(1), |
"n" : { |
"$sum" : NumberInt(1) |
}
|
}
|
}
|
],
|
{
|
"allowDiskUse" : false |
}
|
);
|
Attachments
Issue Links
- backported by
-
SERVER-57518 16% performance loss switching from Count to CountDocuments
-
- Backlog
-