Currently, users who wish to execute near queries against sharded collections must use either the geoNear command or the $geoNear aggregation stage. The $near and $nearSphere query predicates, on the other hand, will fail when issued against a sharded collection. We should add support for sharded $near/$nearSphere, especially given future plans to mark the query the geoNear command as deprecated.
One way to accomplish this would be to make use of the existing "geoNearDistance" $meta projection. In this design, the mongos query path would extend a $near query to project the "geoNearDistance" metadata as teh sort key. The query issued from mongos to the shards would look something like this:
> db.c.find({a: {$nearSphere: [1, 1]}}, {$sortKey: {$meta: "geoNearDistance"}}) { "_id" : ObjectId("599c54f2c4ed9620c11bd27b"), "a" : [ 1, 1 ], "$sortKey" : 0 } { "_id" : ObjectId("599c577621d4e4bc06f8c498"), "a" : [ 1, 2 ], "$sortKey" : 0.0174532925199433 }
The AsyncResultsMerger, which merges query results on mongos, already knows how to interpret $sortKey in order to do a merge-sort.
- is duplicated by
-
SERVER-12399 $geoNear operator does not work in sharded cluster
- Closed
- is related to
-
SERVER-5236 $near query should return results progressively
- Closed
- related to
-
SERVER-32310 Log a warning that the geoNear command is deprecated
- Closed
-
SERVER-926 geo search sharding support
- Closed