TJR
TJR

Reputation: 6577

MongoDB - how to use the best way for GEO and adresses?

I want to find the best way to solve my problem. I don’t know the best way to use MongoDB in my case.

I currently developed a project in Meteor JS and have (want) to use mongoDB with it. On the site is an input field which has autocomplete in it.

(( Only for Germany in my case )) The function should be like this:

You can type your ZIP-Code and it will find the autocompletes with the city in it. e.g. "10789 Berlin"

You also can just type "Berlin" in it or a Street or - if you want - you’re currently Position by browser detecting.

At this time I have a big database in MYSQL with millions of geo data. I have Millions of addresses with the ZIP code, city and GEO-Data. I want to use this to autocomplete the data and to find the GEO code after typing a address.

My question is now how to build my MongoDB-Collection(s). All information in one Collection ? Or do I have to split it in many Collections? e.g. 1 Collection for Zip-Codes, Cities and Streets (with street-number?).

Something like this:

streets:
{
    streetname : 'Sandsteinweg',
    geocode : 'GEOCODE',
    zip : 12349
}

plz : {
    plz : 12349,
    geocode: 'GEOCODE',
    city : berlin

}

citys :
{
    name : Berlin,
    geocode : 'GEOCODE',
    bundesland : 'Berlin'
}

Any ides or input for the best way to do this?

BTW: I don't want to use external services. External service is just in use when the street name is not in my collection and i have to add it with the new Geo-informations which are not in my collection yet.

Upvotes: 4

Views: 316

Answers (1)

Neil Lunn
Neil Lunn

Reputation: 151122

Doing this with MongoDB it would sound like you want to use "text search" or otherwise use a search engine. This is the usual case with actions such as "auto-suggest" (as opposed to "auto-complete") as the "relevant matches" are presented in this case.

So in any rate, you likely want the fields for various types in the same document. At least in the case of the MongoDB text indexes, that will have to be the case, as well as being the easiest integration. And in either case, your "search" will be performed over those multiple field values, and return results in order of relevance.

Largely your question seems to be more about the "auto-suggest" point more that the GEO storage part. There are several options available for "Geo" data and you find them in the documentation.

Yet the possibly conflicting point here is that you can only have something such as a 2dSphere index, once per collection. So that would need to "key" on documents having only one series of "GEO" data per collection.

But there is also nothing wrong with your collection documents being somewhat "polymorphic", so there is that to consider.

It would be difficult to answer your current question in more detail as the subject is fairly broad, without you "narrowing down" to a more specific case. I really only have this here because it is way too long for a comment.

Upvotes: 3

Related Questions