[SERVER-58602] Rewrite $geoNear to $geoWithin + compute distance + $sort Created: 15/Jul/21  Updated: 29/Oct/23  Resolved: 19/Nov/21

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: 5.2.0

Type: Bug Priority: Major - P3
Reporter: David Percy Assignee: David Percy
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
depends on SERVER-59171 Allow {$meta: 'geoNearDistance'} synt... Closed
depends on SERVER-55239 Support 2dsphere index on time-series... Closed
depends on SERVER-58745 Expose computeGeoNearDistance() as an... Closed
is depended on by SERVER-61323 Optimize $geoNear minDistance bounds ... Closed
Duplicate
is duplicated by SERVER-61323 Optimize $geoNear minDistance bounds ... Closed
Related
is related to SERVER-84974 Investigate $geoNear blocking sort Closed
Operating System: ALL
Sprint: QO 2021-09-06, QO 2021-09-20, QO 2021-10-04, QO 2021-10-18, QO 2021-11-01, QO 2021-11-15, QO 2021-11-29
Participants:

 Description   

$geoNear sorts documents by their distance from a given query point. But its implementation is very tied to a particular index-scan plan, so it can only appear as the first stage in a pipeline.

We can lift this restriction by splitting it up into separate stages. Using the new internal-only stage from SERVER-58745, we could rewrite it like this:

{$geoNear: {
  key: <fieldname>,
  near: <point>,
  minDistance: <min>,
  maxDistance: <max>,
}}
 
=>
 
{$match: { <fieldname>: {$geoWithin: {$centerSphere: [<point>, <max>]}} }}
{$match: { <fieldname>: {$not: {$geoWithin: {$centerSphere: [<point>, <min>]}}} }}
{$_internalGeoNearDistance: {
  key: <fieldname>,
  near: <point>,
}}
{$sort: {$meta: 'geoNearDistance'}}

Splitting it up this way will allow other rewrites to optimize the $geoWithin and $sort.



 Comments   
Comment by Githook User [ 19/Nov/21 ]

Author:

{'name': 'David Percy', 'email': 'david.percy@mongodb.com', 'username': 'dpercy'}

Message: SERVER-58602 Implement $geoNear on time-series measurements
Branch: master
https://github.com/mongodb/mongo/commit/86b8db8755d10f86a28a843f501007fe05ff0324

Comment by David Percy [ 21/Jul/21 ]

I think this depends on SERVER-55239, because that will allow us to create a pipeline internally where $geoNear is not the first stage.

Generated at Thu Feb 08 05:44:57 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.