...
I have a query that uses $box in a $geoWithin. I use Google Maps in my app, and whenever a user views a part of the map that spans across the Antemeridian (where part of the viewport is on one side of it and part of it on the other), we get incorrect results. I found this SO issue that basically describes the same problem, but with no relevant answers: https://stackoverflow.com/questions/71799194/bug-of-geowithin-box-mongodb P.S. - Switching to a Polygon solves this, but it really makes sense for my app to use 2d rather than 2dsphere, and switching would also mean handling projection issues, and I couldn't find a Mongo recommended way of doing this.
JIRAUSER1273620 commented on Sun, 23 Jul 2023 07:24:54 +0000: Hi yuan.fang@mongodb.com, Indeed you are correct, and when we use the $geometry operator we specify the CRS explicitly. But in this case we're not using the $geometry operator, we're using the $box operator, which has no way of setting CRS AFAIK. For $geometry this requirement makes sense due to query being potentially vague, but $box should be ordered according to the docs, so MongoDB can understand exactly what the query means. (And for context, we don't want to use $geometry and Polygons because we want to query on a 2d plane) I hope this is clear enough. I still think this is a bug and would love to get your input. Thanks, Omer JIRAUSER1270794 commented on Wed, 19 Jul 2023 14:09:46 +0000: Hi omer@gable.to, The MongoDB document regarding"Big" Polygons explains that: For $geoWithin, if you specify a single-ringed polygon that has an area greater than a single hemisphere, include the custom MongoDB coordinate reference system in the $geometry expression; otherwise, $geoWithin queries for the complementary geometry. In the example you provided, the coordinates [ [142.8925, -67.9184], [-116.5788, -46.5688] ] cover locations that are present in both the eastern and western hemispheres, therefore, it is necessary to include the MongoDB coordinate reference system in the $geometry expression to get the expected result. If you have any further questions, we'd like to encourage you to start by asking our community for help by posting on the MongoDB Developer Community Forums. If the discussion there leads you to suspect a bug in the MongoDB server, then we'd want to investigate it as a possible bug here in the SERVER project. Regards, Yuan JIRAUSER1273620 commented on Sat, 15 Jul 2023 10:14:38 +0000: Hey yuan.fang@mongodb.com, The "incorrect results" are the same as in the StackOverflow issue. I get the "inverse" of the coordinates I ask for (meaning, for example, data between my latitude points, but everything that's NOT between my longitude points). Example query: `$box: [ [142.8925, -67.9184], [-116.5788, -46.5688] ]`. According to the docs, these should represent the bottom left and top right coordinates, but that's clearly not what happens here - it appears MongoDB treats this as if the coordinates represent bottom right and top left. JIRAUSER1270794 commented on Fri, 30 Jun 2023 15:27:49 +0000: Hi omer@gable.to, Thank you for your report. To help us better understand the issue you encountered with $box, could you please provide detailed steps to reproduce the problem? Additionally, kindly elaborate on what are the "incorrect results" and why it is not expected. If the problem you encountered is the same as the link you provided, I think the "Big" Polygons explains, as the polygon span across the eastern and western hemispheres. Regards, Yuan