title: Storing Directory Hierarchy in a Key-Value Data store
body: <p>What is a clean/efficient method for storing the directory Hierarchy/tree in a Key-Value database (in my case MongoDB but any of them)? </p>
  
  <p>For example a tree structure</p>
  
  <pre><code>- Cars 
     + Audi 
     + BMW
        - M5
     + Ford
  - Color
     + Red
        - Apple
        - Cherry
     + Purple
  - Funny
  </code></pre>
  
  <p>The method I am using now, each object links to it's parent</p>
  
  <pre><code>{ 
    dir: "red"
    parent-dir: "color"
  }
  </code></pre>
  
  <p>This makes it very efficient/fast to insert and reorder any aspect of the tree (for example if I want to move Red and all it's children to the Cars directory).  </p>
  
  <p>But this method sucks when I want to all subdirectories and their children for a given directory recursively. To make it efficient to parse I can have a structure for example</p>
  
  <pre><code>{ 
    dir: "red"
    children: "audi, bmw, ford"
  }
  
  { 
    dir: "bmw"
    children: "m5"
  }
  </code></pre>
  
  <p>But if I want to modify the tree, a whole bunch of objects need to touched and modified. </p>
  
  <p>Are there any other methods to storing a directory structure in a KV store?</p>