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
This commit is contained in:
Benjamin Simonsson
2018-12-28 15:45:36 +01:00
committed by Florent Vilmart
parent 66f594342e
commit de92ce5c49

View File

@@ -80,6 +80,16 @@ export default class MongoCollection {
}
count(query, { skip, limit, sort, maxTimeMS, readPreference } = {}) {
// If query is empty, then use estimatedDocumentCount instead.
// This is due to countDocuments performing a scan,
// which greatly increases execution time when being run on large collections.
// See https://github.com/Automattic/mongoose/issues/6713 for more info regarding this problem.
if (typeof query !== 'object' || !Object.keys(query).length) {
return this._mongoCollection.estimatedDocumentCount({
maxTimeMS,
});
}
const countOperation = this._mongoCollection.countDocuments(query, {
skip,
limit,