MongoDB $geoWithin $box operator

85 views
Skip to first unread message

Markus Markussen

unread,
Jan 3, 2020, 7:16:49 PM1/3/20
to mongodb-user
I am currently having a problem understanding how MongoDB does the $geoWithin $box query. Because when I search with a normal 2d index, all values between lower left and upper right is searched (let's say 100k documents). And when I do the same query with the $geoWithin $box operator, it search only for 1/5 of the documents. Why is that?

Kevin Adistambha

unread,
Jan 5, 2020, 7:39:22 PM1/5/20
to mongodb-user

Hi Markus,

My guess is, a normal 2D index treats the points like points on a flat 2D plane. This way, the box is a simple 2D box where you can just get the coordinates of the corners of the box and draw a straight line between them.

In contrast, $geoWithin places the box on a sphere, so you need to take into account the curvature of the sphere (the earth, in terms of geo queries). If you assume a sphere, then drawing a straight line across two points like in a flat 2D plane is not correct anymore. I’m guessing that some of the points you have are actually outside the box when you project the box onto a sphere.

If you don’t think this is the case, could you provide some example documents and the query itself?

Best regards,
Kevin

Markus Markussen

unread,
Jan 6, 2020, 3:09:20 AM1/6/20
to mongodb-user
All my points are within Beijing, so my first thought is that the difference should not be that big. Any thought on this?

Kevin Adistambha

unread,
Jan 6, 2020, 4:19:02 PM1/6/20
to mongodb-user

Hi,

Please post more details:

  • What are the query you tried (geo and 2D)
  • What are the indexes defined in the collection
  • Some example documents that are in the box, and documents that was shown to be outside the box by the geo queries
  • What is your MongoDB version

Best regards,
Kevin

Markus Markussen

unread,
Jan 7, 2020, 2:06:58 AM1/7/20
to mongodb-user
What I tried is a flat 2D query versus a geo query.

What I think I do not understand, is the following: Even though the points are on a curvature, we should still search between all the geohash values between lower left corner and upper right corner, as they are sorted hierarchically. In MongoDB this does not seem to be the case, why is that?

Kevin Adistambha

unread,
Jan 7, 2020, 7:57:12 PM1/7/20
to mongodb-user

Hi Markus,

What I tried is a flat 2D query versus a geo query.

Yes I understand that is the case. Could you post the actual queries and the documents so I can try to reproduce what you’re seeing?

Best regards,
Kevin

Reply all
Reply to author
Forward
0 new messages