Skip to content

Commit de92ce5

Browse files
Benjamin Simonssonflovilmart
Benjamin Simonsson
authored andcommitted
Fix for count being very slow on large Parse Classes' collections (#5264)
* * Added fix for MongoCollection's count function, so that it uses the much more effecient estimatedDocumentCount if no queries were specified * * Added missing options when running estimatedDocumentCount for Mongo Collections * * Fixed issue with checking for zero query for Mongo Collections count
1 parent 66f5943 commit de92ce5

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/Adapters/Storage/Mongo/MongoCollection.js

+10
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ export default class MongoCollection {
8080
}
8181

8282
count(query, { skip, limit, sort, maxTimeMS, readPreference } = {}) {
83+
// If query is empty, then use estimatedDocumentCount instead.
84+
// This is due to countDocuments performing a scan,
85+
// which greatly increases execution time when being run on large collections.
86+
// See https://github.com/Automattic/mongoose/issues/6713 for more info regarding this problem.
87+
if (typeof query !== 'object' || !Object.keys(query).length) {
88+
return this._mongoCollection.estimatedDocumentCount({
89+
maxTimeMS,
90+
});
91+
}
92+
8393
const countOperation = this._mongoCollection.countDocuments(query, {
8494
skip,
8595
limit,

0 commit comments

Comments
 (0)