how to get all keys in mongodb code example
Example 1: mongodb list all keys
var getKeys = function(collName) { var keys = {}; db.getCollection(collName).find().forEach(function(value) { Object.keys(value).forEach(function (keyName) { if(keys[keyName]) { keys[keyName] += 1; return; } keys[keyName] = 1; }); }); return keys; }
getKeys('zips')
Example 2: mongodb get keys of document
var allKeys = {}; db.YOURCOLLECTION.find().forEach(function(doc){Object.keys(doc).forEach(function(key){allKeys[key]=1})}); allKeys;