L
L
likejavascript2015-03-19 15:35:57
MongoDB
likejavascript, 2015-03-19 15:35:57

How to properly search for documents by city/country using MongoDB and GoogleMaps APIv3?

I am implementing a system for storing and searching ads like Airbnb and using MongoDB as a database. In the ad itself, I store the latitude / longitude coordinates that the user specifies using GoogleMaps Places Autocomplete
and the document data in the database looks like this:

{
    "_id": {
        "$oid": "54f34c8bbb465c1d119bef49"
    },
    "author": {
        "$oid": "54ea447c23f6f81f676083c9"
    },
    'Заголовок объявления',
    "location": {
        "name": "Санкт-Петербург, Россия",
        "loc": [
            30.335098600000038,
            59.9342802
        ]
    },
    "created_at": {
        "$date": "2015-03-01T17:29:47.603Z"
    },
    "__v": 0
}

Question: how to find a list of all ads if the user scores for example "Russia"? That is, you need to find all ads with addresses that were created within Russia.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Kitmanov, 2015-03-19
@k12th

You need to have a polygon in geoJSON format that represents the outlines of Russia, and select those documents that fall into it using $geoWithin . If this turns out to be too long a query, you can select by ownership of the bounding box first.
Where to store this polygon is the tenth thing, even in the same mong in another collection, even just in jason. These polygons can be taken by downloading the vector outlines of countries in .shp and converting them using qgis, it is not difficult to google.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question