From 30fcb0ae53f49369ac1e5ecfc6a3ce4bbffa1f48 Mon Sep 17 00:00:00 2001 From: Benjamin Simonsson Date: Fri, 28 Dec 2018 10:36:29 +0100 Subject: [PATCH 1/3] * Added fix for MongoCollection's count function, so that it uses the much more effecient estimatedDocumentCount if no queries were specified --- src/Adapters/Storage/Mongo/MongoCollection.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Adapters/Storage/Mongo/MongoCollection.js b/src/Adapters/Storage/Mongo/MongoCollection.js index bcc774aff9..45cf162133 100644 --- a/src/Adapters/Storage/Mongo/MongoCollection.js +++ b/src/Adapters/Storage/Mongo/MongoCollection.js @@ -80,6 +80,11 @@ export default class MongoCollection { } count(query, { skip, limit, sort, maxTimeMS, readPreference } = {}) { + // If query is empty, then use estimatedDocumentCount instead. + if (typeof (query) !== 'object' || Object.keys(query).length) { + return this._mongoCollection.estimatedDocumentCount(); + } + const countOperation = this._mongoCollection.countDocuments(query, { skip, limit, From 19c199427cde38bcb05833d8f49c2d738367cb30 Mon Sep 17 00:00:00 2001 From: Benjamin Simonsson Date: Fri, 28 Dec 2018 10:50:39 +0100 Subject: [PATCH 2/3] * Added missing options when running estimatedDocumentCount for Mongo Collections --- src/Adapters/Storage/Mongo/MongoCollection.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Adapters/Storage/Mongo/MongoCollection.js b/src/Adapters/Storage/Mongo/MongoCollection.js index 45cf162133..36bd0a94f3 100644 --- a/src/Adapters/Storage/Mongo/MongoCollection.js +++ b/src/Adapters/Storage/Mongo/MongoCollection.js @@ -81,8 +81,13 @@ 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(); + return this._mongoCollection.estimatedDocumentCount({ + maxTimeMS, + }); } const countOperation = this._mongoCollection.countDocuments(query, { From 7995b85b056553c73e8e8239552b95db62b3517b Mon Sep 17 00:00:00 2001 From: Benjamin Simonsson Date: Fri, 28 Dec 2018 12:57:19 +0100 Subject: [PATCH 3/3] * Fixed issue with checking for zero query for Mongo Collections count --- src/Adapters/Storage/Mongo/MongoCollection.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Adapters/Storage/Mongo/MongoCollection.js b/src/Adapters/Storage/Mongo/MongoCollection.js index 36bd0a94f3..50e7a41123 100644 --- a/src/Adapters/Storage/Mongo/MongoCollection.js +++ b/src/Adapters/Storage/Mongo/MongoCollection.js @@ -84,7 +84,7 @@ export default class MongoCollection { // 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) { + if (typeof query !== 'object' || !Object.keys(query).length) { return this._mongoCollection.estimatedDocumentCount({ maxTimeMS, });