MongoDB
 sql >> Database >  >> NoSQL >> MongoDB

Iterare su tutto il database Mongo

Puoi usare db.getSiblingDB() per passare da un database all'altro e db.getCollectionNames() per ottenere i nomi della collezione. Nota che devi eseguire il primo comando da admin database per ottenere l'elenco dei database. Un breve script nella shell per ottenere ciò che vuoi fare sarebbe simile al seguente:

// 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);
    });

});