Skip to content

Instantly share code, notes, and snippets.

@korsmakolnikov
Last active April 29, 2016 16:54
Show Gist options
  • Save korsmakolnikov/f12d50597aa4215d2c59a11614f89804 to your computer and use it in GitHub Desktop.
Save korsmakolnikov/f12d50597aa4215d2c59a11614f89804 to your computer and use it in GitHub Desktop.
Program to scan an entire mongo database for a certain fieldname (and check recursevely in embedded docs)
var collections = db.getCollectionNames();
//print(collections);
collections.forEach(function(col) {
function isEmpty(obj) {
for(var prop in obj) {
if(obj.hasOwnProperty(prop))
return false;
}
return true;
}
function professioneScan(col) {
var result = [];
var count = 0;
db.getCollection(col).find({}).forEach(function scan(doc) {
for(var key in doc) {
print(typeof doc[key]);
/*if((typeof doc[key]) === 'object') {
scan(doc[key]);
} else {
if(key==='professione') {
result[col] = {};
result[col][key]=count;
count++;
}
}*/
}
});
if(!isEmpty(result))
print(result);
}
professioneScan(col);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment