Reputation: 107
I recently started working on mongodb. My simple application will have Geo hierarchy like
Continet-->Country-->Region--->Resort-->Accommodation
My data will be like this
{
"_id" : ObjectId("54854d5af2ef42fe7d4d2a44"),
"name" : "Europe",
"parent" : "",
"type" : "Continent",
"synonym" : "",
"code" : "EEE"
}
{
"_id" : ObjectId("54854d5af2ef42fe7d4d2a47"),
"name" : "Bulgaria",
"parent" : "EEE",
"type" : "Country",
"synonym" : "",
"code" : "BGR"
}
{
"_id" : ObjectId("54854d5af2ef42fe7d4d2a45"),
"name" : "Albania",
"parent" : "EEE",
"type" : "Country",
"synonym" : "",
"code" : "ALB"
}
{
"_id" : ObjectId("54854d5af2ef42fe7d4d2a48"),
"name" : "Bourgas Region",
"parent" : "BGR",
"type" : "Region",
"synonym" : "",
"code" : "002682"
}
{
"_id" : ObjectId("54854d5af2ef42fe7d4d2a49"),
"name" : "Obzor",
"parent" : "002682",
"type" : "Resort",
"synonym" : "",
"code" : "002911"
}
{
"_id" : ObjectId("54854d5af2ef42fe7d4d2a4a"),
"name" : "Sunny Beach",
"parent" : "002682",
"type" : "Resort",
"synonym" : "",
"code" : "002548"
}
{
"_id" : ObjectId("54854d5af2ef42fa7d4d2b3d"),
"name" : "Hotel Park Avenue",
"parent" : "002548",
"type" : "Accommodation",
"synonym" : "",
"code" : "022548"
}
As shown above there will be only the direct parent available in every document.
Now I have to get all the parents and children to a document with "code" : "002548"
If I have the Resort code, i have to get all the Accommodations which have the respective resort as parent and i have to get the Destination, Country, Continent to which the resort belongs to.
I feel that there should be recursive queries to get this, I am using MongoTemplate for db operations.
Please Help me to get the best solution for this.
Thanks in advance.
Upvotes: 1
Views: 1967
Reputation: 11671
It'd be helpful to know what kinds of queries you're running on these documents, but I'm guessing the levels of interest are Resort
and Accomodation
. You've structured the higher three levels as separate documents, but it seems to me they are properties of Resorts
and Accomodations
: a Resort
is in a given region, country, continent. Moreover, regions, countries, and continents don't change very much. I think the higher-level geo information is a good candidate for being embedded in Resort
and Accommodation
documents:
{
"_id" : ObjectId("54854d5af2ef42fa7d4d2b3d"),
"name" : "Hotel Park Avenue",
"type" : "Accommodation",
"synonym" : "",
"code" : "022548",
"region" : "New York",
"country" : "United States",
"continent" : "North America"
}
With this kind of structure, finding the "parents" of an accommodation like the above is just looking at the region, country, continent field values. Finding the "children" of, e.g., a country is a simple query:
db.stuff.find({ "country" : "United States" })
Upvotes: 0
Reputation: 6233
mongodb doesn't support joins so you'll have to do multiple queries to walk up that hierarchy.
Upvotes: 3