Skip to content

Instantly share code, notes, and snippets.

@leakypixel
Last active May 11, 2017 15:45
Show Gist options
  • Save leakypixel/af9b28abb288e202af0cdab7b913f8e8 to your computer and use it in GitHub Desktop.
Save leakypixel/af9b28abb288e202af0cdab7b913f8e8 to your computer and use it in GitHub Desktop.
Search all collections and fields in mongoDB
function SearchAll(regexp) {
var results = {};
var all = db.getCollectionNames();
var results = [];
for (var i in all) {
var coll = all[i];
if (coll == "system.indexes") continue;
results[coll] = [];
db[coll].find().forEach(
function(doc) {
Object.keys(doc).forEach(
function(key) {
if (regexp.test(doc[key])) {
results[coll].push(doc);
};
}
)
}
);
}
return results;
}
SearchAll(/tyres/gi);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment