Tuesday 25 June 2013

MongoDB Get names of all keys in collection

mr = db.runCommand({ 

 "mapreduce" : "things", 

 "map" : function() {

   for (var key in this) { 

         emit(key, null); } },

 "reduce" : function(key, stuff) { 

  return null; }, "out": "things" + "_keys" })

Then run distinct on the resulting collection so as to find all the keys:

db[mr.result].distinct("_id") ["foo", "bar", "baz", "_id", ...]

No comments: