mongoose - How do i get all keys in Mongodb collection? -
i saw few solutions not exact solution. have db name results , collection name marks below:
db.marks.find(); { "_id" : objectid("54f57522627af4bfdcf79764"), "name" : "john", "scroe1" : 23, "score2" : 21, "score5" : 12 } { "_id" : objectid("54f5761a627af4bfdcf79765"), "name" : "mike", "scroe2" : 22, "score3" : 20, "score4" : 22 } { "_id" : objectid("559d0bc521cb2e056507c3e3"), "name" : "bush", "score2" : 30 } i tried with
var doc=db.marks.findone(); (var key in doc) print(key); and got
_id name score1 score2 score5 but want keys in collection below:
_id, name, score1, score2, score3, score4, score5 name scroe1 score2 score3here
findone return first found document. since first document list not have score3 , score4 keys, not display them. if want show root-level keys across documents, need iterate through documents in db.
var keys = []; db.marks.find().foreach(function(doc){ (var key in doc){ if(keys.indexof(key) < 0){ keys.push(key); } } }); print(keys);
Comments
Post a Comment