Thursday, March 5, 2015

Get the list of name of fields in mongo using pymongo and mongodb shell

followings will list all fields exist in a collection in mongodb :

Pymongo: 

#Using pymongo and connection named 'db'

reduce(
    lambda all_keys, rec_keys: all_keys | set(rec_keys), 
    map(lambda d: d.keys(), db.things.find()), 
    set()
)

Mongodb Shell:

mr = db.runCommand(
{"mapreduce" : "temp",  
"map" : function() { for (var key in this) { emit(key, null); } }, "reduce" : function(key, stuff) { return null; }, "out": "temp" + "_keys" }) 
db[mr.result].distinct("_id")




No comments:

Post a Comment