Suitability of MongoDB for hierarchial type queries?

Note that this question was also asked on the google group. See groups.google.com/group/mongodb-user/bro... for that disucssion.

Note that this question was also asked on the google group. See groups.google.com/group/mongodb-user/bro... for that disucssion. One option is to use an array key.

You can store the hierarchy as an array of values (for example 'US','CA','Los Angeles'). Then you can query against records based on individual elements in that array key For example: First, store some documents with the array value representing the hierarchy > db.hierarchical. Save({ location: 'US','CA','LA', name: 'foo'} ) > db.hierarchical.

Save({ location: 'US','CA','SF', name: 'bar'} ) > db.hierarchical. Save({ location: 'US','MA','BOS', name: 'baz'} ) Make sure we have an index on the location field so we can perform fast queries against its values > db.hierarchical. EnsureIndex({'location':1}) Find all records in California > db.hierarchical.

Find({location: 'CA'}) { "_id" : ObjectId("4d9f69cbf88aea89d1492c55"), "location" : "US", "CA", "LA" , "name" : "foo" } { "_id" : ObjectId("4d9f69dcf88aea89d1492c56"), "location" : "US", "CA", "SF" , "name" : "bar" } Find all records in Massachusetts > db.hierarchical. Find({location: 'MA'}) { "_id" : ObjectId("4d9f6a21f88aea89d1492c5a"), "location" : "US", "MA", "BOS" , "name" : "baz" } Find all records in the US > db.hierarchical. Find({location: 'US'}) { "_id" : ObjectId("4d9f69cbf88aea89d1492c55"), "location" : "US", "CA", "LA" , "name" : "foo" } { "_id" : ObjectId("4d9f69dcf88aea89d1492c56"), "location" : "US", "CA", "SF" , "name" : "bar" } { "_id" : ObjectId("4d9f6a21f88aea89d1492c5a"), "location" : "US", "MA", "BOS" , "name" : "baz" } Note that in this model, your values in the array would need to be unique.So for example, if you had 'springfield' in different states, then you would need to do some extra work to differentiate.

> db.hierarchical. Save({location:'US','MA','Springfield', name: 'one' }) > db.hierarchical. Save({location:'US','IL','Springfield', name: 'two' }) > db.hierarchical.

Find({location: 'Springfield'}) { "_id" : ObjectId("4d9f6b7cf88aea89d1492c5b"), "location" : "US", "MA", "Springfield", "name" : "one" } { "_id" : ObjectId("4d9f6b86f88aea89d1492c5c"), "location" : "US", "IL", "Springfield", "name" : "two" } You can overcome this by using the $all operator and specifying more levels of the hierarchy. For example: > db.hierarchical. Find({location: { $all : 'US','MA','Springfield'} }) { "_id" : ObjectId("4d9f6b7cf88aea89d1492c5b"), "location" : "US", "MA", "Springfield", "name" : "one" } > db.hierarchical.

Find({location: { $all : 'US','IL','Springfield'} }) { "_id" : ObjectId("4d9f6b86f88aea89d1492c5c"), "location" : "US", "IL", "Springfield", "name" : "two" }.

Thanks; I understand what you have shown. However, how would I get the results for "give me all the vendors who map to Springfield | MA | US"? (| being binary OR, given us a set union).

– IanC Apr 9 at 4:55 Isn't that just db.hierarchicla. Find({location:'US'})? MA and Springfield are subsets so no OR or union is required.

– jared Apr 9 at 21:35 No. Look at the example I've added to the question. – IanC Apr 10 at 7:24.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions