M
M
My joy2015-01-10 18:49:44
MongoDB
My joy, 2015-01-10 18:49:44

Why is mongodb not finding a geopoint?

Good evening everyone!
I have a document whose coordinates are in the center of Moscow (TsUM), I added the 2dsphere index to its loc variable and all without errors. But when trying to find it through $box (the search scope is all Moscow time), mongo returns nothing. What am I doing wrong?
Here is the document:
3b7bd5c86d494e139159b8388cf7b6d6.PNG
Here is the search query:
d53516d294c64a218e279f9da38c4d27.PNG
$within changed to $geoWithin, lat and lon changed places already. Nothing helps...
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nowm, 2015-01-10
@t-alexashka

Edit: removed all previous text.
The bottom line is that for $box you need to set the coordinates in this way:

$box: [
    [ <нижний левый угол> ],
    [ <верхний правый угол> ]
]

In this case, the longitude (longtitude) must be indicated first, and then the latitude (latitude). For Moscow, the longitude, roughly speaking, is 37, and the latitude is 55 (again, in a rough approximation).
To understand where the lower left and upper right corners will be, you need to imagine the whole situation like this: plus longitude starts from London and goes to the right. Positive latitude starts from the equator and goes up. So the point with coordinates (0, 0) will be to the left and below Moscow. The bottom left corner will be the minimum latitude and longitude, and the top right corner will be the maximum latitude and longitude.
To make the correct coordinates for $box, you need to take the minimum values ​​​​of latitude and longitude for the lower left corner - [36.362, 55.38691], and for the upper right - the maximum,[38.573, 56.00606]. As a result, this query covers the point you were looking for:
db.places.find({
    loc: {
        $within: {
            $box: [
                [36.362, 55.38691], 
                [38.573, 56.00606]
            ]
        } 
    } 
})

On my machine, anyway, it works fine, and the desired point falls within the range.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question