Skip to content

Commit 483bdbe

Browse files
committed
Apply conditional estimatedDocumentCount & countDocuments (PR parse-community#5264)
1 parent 98520e0 commit 483bdbe

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/Adapters/Storage/Mongo/MongoCollection.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,18 @@ export default class MongoCollection {
5555
}
5656

5757
count(query, { skip, limit, sort, maxTimeMS, readPreference } = {}) {
58-
const countOperation = this._mongoCollection.count(query, { skip, limit, sort, maxTimeMS, readPreference });
58+
//ref: https://github.com/parse-community/parse-server/pull/5264/files#diff-271c623e43af83bc3d858c5ad248451a
59+
// If query is empty, then use estimatedDocumentCount instead.
60+
// This is due to countDocuments performing a scan,
61+
// which greatly increases execution time when being run on large collections.
62+
// See https://github.com/Automattic/mongoose/issues/6713 for more info regarding this problem.
63+
if (typeof query !== 'object' || !Object.keys(query).length) {
64+
return this._mongoCollection.estimatedDocumentCount({
65+
maxTimeMS,
66+
});
67+
}
68+
69+
const countOperation = this._mongoCollection.countDocuments(query, { skip, limit, sort, maxTimeMS, readPreference });
5970

6071
return countOperation;
6172
}

0 commit comments

Comments
 (0)