Skip to content

Commit 720ea5f

Browse files
committed
Fix parse-community#5285 by rewriting query (replacing $nearSphere by $geoWithin)
All credit goes to @dplewis !
1 parent c18bf7b commit 720ea5f

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/Adapters/Storage/Mongo/MongoCollection.js

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

8282
count(query, { skip, limit, sort, maxTimeMS, readPreference } = {}) {
83+
// We have to replace $nearSphere in query by $geoWithin with $centerSphere
84+
// see MongoDB doc: http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#countDocuments
85+
// This is tied to issue #5285 https://github.com/parse-community/parse-server/issues/5285
86+
for (const key in query) {
87+
if (query[key].$nearSphere) {
88+
const geoQuery = {
89+
$geoWithin: {
90+
$centerSphere: [query[key].$nearSphere, query[key].$maxDistance],
91+
},
92+
};
93+
query[key] = geoQuery;
94+
}
95+
}
96+
8397
// If query is empty, then use estimatedDocumentCount instead.
8498
// This is due to countDocuments performing a scan,
8599
// which greatly increases execution time when being run on large collections.

0 commit comments

Comments
 (0)