title: mongodb: create a top-level index for a nested document instead of having to index each individual sublevel?
body: <p>This question is about how I can use indexes in MongoDB to look something up in nested documents, without having to index each individual sublevel.
  I have a collection "test" in MongoDB which basically goes something like this:</p>
  
  <pre><code>{
  "_id" : ObjectId("50fdd7d71d41c82875a5b6c1"),
  "othercol" : "bladiebla",
  "scenario" : {
          "1" : { [1,2,3] },
          "2" : { [4,5,6] }
  }}
  </code></pre>
  
  <p>Scenario has multiple keys, each document can have any subset of the scenarios (i.e. from none to a subset to all). Also: Scenario can't be an array because i need it as a dictionary in Python. I created an index on the "scenario" field.<br>
  My issue is that i want to select on the collection, filtering for documents that have a certain value. So this works fine functionally:</p>
  
  <pre><code>db.test.find({"scenario.1": {$exists: true}})
  </code></pre>
  
  <p>However, it won't use any index i've put on scenario. Only if i put an index on the "scenario.1" an index is used. But I can have thousands (or more) scenarios (and the collection itself has 100.000s of records), so i would prefer not to!<br>
  So i tried alternatives:</p>
  
  <pre><code>db.test.find({"scenario": "1"}) 
  </code></pre>
  
  <p>This will use the index on scenario, but won't return results. Making scenario an array still gives the same index issue.<br></p>
  
  <p>Is my question clear? Can anyone give a pointer on how I could achieve the best performance here?</p>
  
  <p>P.s. I have seen this: <a href="https://stackoverflow.com/questions/9730136/how-to-create-a-nested-index-in-mongodb">How to Create a nested index in MongoDB?</a> but that solution is not possible in my case (due to the amount of scenarios)</p>