Iterate through all collections of all the dbs in MongoDB


Iterate through all collections of all the dbs in MongoDB



I need to Iterate through all the collections of each db. For each collection i need to do a collectionName.find() and print the json



Below is what i have tried:


#db_get_all_collections.js

db.adminCommand("listDatabases").databases.forEach(function(d) {
mdb = db.getSiblingDB(d.name);
mdb.getCollectionNames().forEach(function(c) {
s = mdb[c].find();
prinjson(s)
}
});



I am dumping the output to a file as below


mongo admin -u <<user_name>> -p <<password>> < db_get_all_collections.js > output.json



However in output.json i see


MongoDB shell version: 3.0.3
connecting to: admin

bye




1 Answer
1


db.adminCommand('listDatabases').databases.forEach(function(d) {
mdb = db.getSiblingDB(d.name);
mdb.getCollectionNames().forEach(function(c) {
var s = mdb.getCollection(c).find().toArray();
printjson(s)
})
})

mongo admin -u <<user_name>> -p <<password>> db_get_all_collections.js > output.json






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to input without newline? (Python)

C++ thread error: no type named ‘type’ MINGW

Analog for TagView in flutter