title: Using map/reduce for mapping the properties in a collection
body: <p><em>Update:</em> follow-up to <a href="https://stackoverflow.com/questions/2298870/mongodb-get-names-of-all-keys-in-collection">MongoDB Get names of all keys in collection</a>.</p>
  
  <p>As pointed out by <a href="https://stackoverflow.com/users/4243/kristina">Kristina</a>, one can use Mongodb 's map/reduce to list the keys in a collection:</p>
  
  <pre><code>db.things.insert( { type : ['dog', 'cat'] } );
  db.things.insert( { egg : ['cat'] } );
  db.things.insert( { type :  [] }); 
  db.things.insert( { hello : []  } );
  
  mr = db.runCommand({"mapreduce" : "things",
  "map" : function() {
      for (var key in this) { emit(key, null); }
  },  
  "reduce" : function(key, stuff) { 
     return null;
  }}) 
  
  db[mr.result].distinct("_id")
  
  //output: [ "_id", "egg", "hello", "type" ]
  </code></pre>
  
  <p>As long as we want to get only the keys located at the first level of depth, this works fine. However, it will fail retrieving those keys that are located at deeper levels. If we add a new record:</p>
  
  <pre><code>db.things.insert({foo: {bar: {baaar: true}}})
  </code></pre>
  
  <p>And we run again the map-reduce +distinct snippet above, we will get:</p>
  
  <pre><code>[ "_id", "egg", "foo", "hello", "type" ] 
  </code></pre>
  
  <p>But we will not get the <em>bar</em> and the <em>baaar</em> keys, which are nested down in the data structure. The question is: how do I retrieve all keys, no matter their level of depth? Ideally, I would actually like the script to walk down to all level of depth, producing an output such as:</p>
  
  <pre><code>["_id","egg","foo","foo.bar","foo.bar.baaar","hello","type"]      
  </code></pre>
  
  <p>Thank you in advance!</p>