Last active
May 11, 2017 15:45
-
-
Save leakypixel/af9b28abb288e202af0cdab7b913f8e8 to your computer and use it in GitHub Desktop.
Search all collections and fields in mongoDB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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