Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In the link from dm's post Dwight Merriman mentions using a path key and doing regex matches</p> <pre><code>{ path : "a.b.c.d.e.f" } </code></pre> <p>Another way to do this would be with arrays</p> <pre><code>{ path : ["a", "b", "c", "d", "e", "f"] } db.test.ensureIndex({path: 1}) </code></pre> <p>that should make it pretty fast. </p> <p>if each node can only be in a single path then you wouldn't need to do worry about where it is located in the list</p> <pre><code>db.test.find({path: "a"}) </code></pre> <p>would find all children of "a"</p> <p>Instead of path names I would probably use the _id of the nodes.</p> <p><strong>Update</strong></p> <ul> <li>one thing to be careful of is that an index can only have one array in it. </li> <li><p>Be careful to use explain on your queries</p> <p>db.test.find({path: {$in: ["a", "b"]})</p></li> </ul> <p>gives you </p> <pre><code> db.test.find({path: {$in: ["a", "b"]}}).explain() { "cursor" : "BtreeCursor path_1 multi", "nscanned" : 2, "nscannedObjects" : 2, "n" : 1, "millis" : 0, "nYields" : 0, "nChunkSkips" : 0, "isMultiKey" : true, "indexOnly" : false, "indexBounds" : { "path" : [ [ "a", "a" ], [ "b", "b" ] ] } } </code></pre> <p>but </p> <pre><code> db.test.find({path: {$all: ["a", "b"]}}).explain() { "cursor" : "BtreeCursor path_1", "nscanned" : 1, "nscannedObjects" : 1, "n" : 1, "millis" : 0, "nYields" : 0, "nChunkSkips" : 0, "isMultiKey" : true, "indexOnly" : false, "indexBounds" : { "path" : [ [ "a", "a" ] ] } } </code></pre> <p>only uses the first element and then scans all matching results for b.<br> If a is your root element or is in most of your records then your doing a nearly full scan of the records instead of an efficient index query.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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