Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Note that this question was also asked on the google group. See <a href="http://groups.google.com/group/mongodb-user/browse_thread/thread/5cd5edd549813148" rel="nofollow">http://groups.google.com/group/mongodb-user/browse_thread/thread/5cd5edd549813148</a> for that disucssion. </p> <p>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 </p> <pre><code>&gt; db.hierarchical.save({ location: ['US','CA','LA'], name: 'foo'} ) &gt; db.hierarchical.save({ location: ['US','CA','SF'], name: 'bar'} ) &gt; db.hierarchical.save({ location: ['US','MA','BOS'], name: 'baz'} ) </code></pre> <p>Make sure we have an index on the location field so we can perform fast queries against its values </p> <pre><code>&gt; db.hierarchical.ensureIndex({'location':1}) </code></pre> <p>Find all records in California </p> <pre><code>&gt; db.hierarchical.find({location: 'CA'}) { "_id" : ObjectId("4d9f69cbf88aea89d1492c55"), "location" : [ "US", "CA", "LA" ], "name" : "foo" } { "_id" : ObjectId("4d9f69dcf88aea89d1492c56"), "location" : [ "US", "CA", "SF" ], "name" : "bar" } </code></pre> <p>Find all records in Massachusetts </p> <pre><code>&gt; db.hierarchical.find({location: 'MA'}) { "_id" : ObjectId("4d9f6a21f88aea89d1492c5a"), "location" : [ "US", "MA", "BOS" ], "name" : "baz" } </code></pre> <p>Find all records in the US </p> <pre><code>&gt; 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" } </code></pre> <p>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. </p> <pre><code>&gt; db.hierarchical.save({location:['US','MA','Springfield'], name: 'one' }) &gt; db.hierarchical.save({location:['US','IL','Springfield'], name: 'two' }) &gt; db.hierarchical.find({location: 'Springfield'}) { "_id" : ObjectId("4d9f6b7cf88aea89d1492c5b"), "location" : [ "US", "MA", "Springfield"], "name" : "one" } { "_id" : ObjectId("4d9f6b86f88aea89d1492c5c"), "location" : [ "US", "IL", "Springfield"], "name" : "two" } </code></pre> <p>You can overcome this by using the $all operator and specifying more levels of the hierarchy. For example: </p> <pre><code>&gt; db.hierarchical.find({location: { $all : ['US','MA','Springfield']} }) { "_id" : ObjectId("4d9f6b7cf88aea89d1492c5b"), "location" : [ "US", "MA", "Springfield"], "name" : "one" } &gt; db.hierarchical.find({location: { $all : ['US','IL','Springfield']} }) { "_id" : ObjectId("4d9f6b86f88aea89d1492c5c"), "location" : [ "US", "IL", "Springfield"], "name" : "two" } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload