Gcoop
Gcoop

Reputation: 3382

NoSQL Documents for Relational Like Data (Locations)

I have a list of locations, which currently in MySQL has a hierarchy above it like so Continent > Country > County/Region/State > Provence > Location. If I were to store these locations in a NoSQL store (like CouchDB). Would I just store the continent as the top level object and the rest as child arrays of objects for this continent. Then use map/reduce to create the various views for countries, counties etc. Is this the recommended structure for this kind of data? It would make each continent document very big?

{
  name: "Europe",
  type: "CONTINENT",
  countries: [
     { 
       name: "England" 
       counties: [...]
     }
  ]
}

Upvotes: 1

Views: 236

Answers (1)

Dominic Barnes
Dominic Barnes

Reputation: 28429

This is only a suggestion and speculation on my part, but I think I may have a good idea. (I would also highly recommend looking into GeoCouch for location-type data.)

If you want to proceed yourself, you can store each location with all of the levels of detail stored as attributes:

{
    "continent": "North America",
    "country": "United States",
    "state": "Texas",
    "city": "Houston"
}

I'm from the US, so I'm using what I would normally use for determining my location. (I noticed you used provence, county, etc. which can easily be incorporated into this model)

Anyways, the view function would look like this:

function (doc) {
    emit([doc.continent, doc.country, doc.state, doc.city], null);
}

This view will output:

{
    "key": ["North America", "United States", "Texas", "Houston"],
    "value": null
}

Now, I know this is redundant data, since it's stored like this for every location. That's OK, we're dealing with NoSQL, there's no need to continue thinking relationally. (that's why you're looking at another solution like CouchDB)

Anyways, you can use the view parameter group_level to "drill-down" into your locations. Using group_level=1 will get you a view result grouped by Continent. group_level=2 will group by Country, group_level=3 will group by State, etc.

Using a reduce function can get you counts and other statistics for each of these groupings. I hope this helps!

Upvotes: 4

Related Questions