📜  foreach db mongodb - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:07.093000             🧑  作者: Mango

代码示例1
// Switch to admin database and get list of databases.
db = db.getSiblingDB("admin");
dbs = db.runCommand({ "listDatabases": 1 }).databases;
// Iterate through each database and get its collections.
dbs.forEach(function(database) {
    db = db.getSiblingDB(database.name);
    cols = db.getCollectionNames();
    // Iterate through each collection.
    cols.forEach(function(col) {
        // Do something with each collection.
        print(col);
    });
});