title: Best way to store inheritance type configuration in mongodb?
body: <p>I store four different variations of configuration in mongo as a nested document.</p>
  
  <ol>
  <li>default configuration</li>
  <li>environment specific configuration.</li>
  <li>site specific configuration.</li>
  <li>page specific configuration.</li>
  </ol>
  
  <p>Precedence : default &lt; environment &lt; site &lt; page.
  The configuration which has greater precedence can override the fields in the lower configuration.</p>
  
  <p>For example : 
  default configuration: </p>
  
  <pre><code>{
   "a":{
     "b":"c"
    }
   "default":{
     }
  } 
  </code></pre>
  
  <p>Env configuration:</p>
  
  <pre><code>{
    "a": {
      "b":"c" 
    }
  }
  </code></pre>
  
  <p>Site configuration:</p>
  
  <pre><code>{
    "a": {
      "b":"c"
    }
  }
  </code></pre>
  
  <p>Page configuration:</p>
  
  <pre><code>{
    "a": {
      "b":"c"
    }
  }
  </code></pre>
  
  <p>So, whenever a configuration for a page is requested i have to query all four documents and merge them.</p>
  
  <p>This would be the merged document if a configuration for a page is requested:</p>
  
  <pre><code>{
    "a":{
      "b":"c"
    },
    "default": {
    }
  }
  </code></pre>
  
  <p>So, if my page is requested a million times, then for each page requested i should fetch all four documents and merge based on their precedence.</p>
  
  <p>How to best store this type of data,how can i remove four db calls for every page load.Is there a better way to store such configuration data?</p>