如何使用Java在Mongo中列出数据库/集合/文档

时间:2018-07-13 08:59:38

标签: java database mongodb

下面是我的代码

    import com.mongodb.MongoClient;
    import com.mongodb.client.*;
    import org.bson.Document;

    import java.util.ArrayList;
    import java.util.List;

    public class MongoMain {

        static MongoClient mongoClient = null;
        static MongoDatabase mdb = null;
        static MongoCollection<Document> collection = null;

        public static void main(String args[]){

            try {

                mongoClient = new MongoClient("localhost", 27017);
                mdb = mongoClient.getDatabase("STAJDB");
                collection = mdb.getCollection("users");
                listAllDatabaseNames();

            } catch (Exception e) {

                e.printStackTrace();

            } finally {

                mongoClient.close();

            }

        }

        public static void listAllDatabaseNames(){
            MongoCursor<String> databases = mongoClient.listDatabaseNames().iterator();
            while(databases.hasNext()) {
                System.out.println(databases);
                listAllCollections(databases.toString());
                databases.next();
            }
        }

        public static void listAllCollections(String databaseName){
            MongoDatabase currentDb = mongoClient.getDatabase(databaseName);
            MongoCursor<String> collections = currentDb.listCollectionNames().iterator();
            while(collections.hasNext()){
                System.out.println("\t" + collections);
                listALLDocuments(collections.toString());
                collections.next();
            }
        }

        public static void listALLDocuments(String colName){
            MongoCollection<Document> col = mdb.getCollection(colName);
            List<Document> documents = col.find().into(new ArrayList<Document>());
            for(Document doc : documents){
                System.out.println("\t\t" + doc.toString());
            }
        }
    }

让我们假设我有4个名为A,B和C的数据库。每个数据库都有1个集合,每个集合都有1个文档。我想这样列出他们。

    A
        a1
            a2
    B  
        b1
            b2
    C
        c1
            c2

数据库名称和文档名称按预期工作,但是当我尝试通过打印集合名称来执行此操作时,却遇到此异常。

 com.mongodb.client.internal.MongoMappingCursor@610694f1
        java.lang.IllegalArgumentException: state should be: databaseName does not contain '.'
        at com.mongodb.assertions.Assertions.isTrueArgument(Assertions.java:99)
        at com.mongodb.MongoNamespace.checkDatabaseNameValidity(MongoNamespace.java:61)
        at com.mongodb.client.internal.MongoDatabaseImpl.<init>(MongoDatabaseImpl.java:68)
        at com.mongodb.MongoClient.getDatabase(MongoClient.java:533)
        at com.ibrahim.MongoMain.listAllCollections(MongoMain.java:47)
        at com.ibrahim.MongoMain.listAllDatabaseNames(MongoMain.java:41)
        at com.ibrahim.MongoMain.main(MongoMain.java:23)
        Jul 13, 2018 11:51:35 AM com.mongodb.diagnostics.logging.JULLogger log
        INFO: Closed connection [connectionId{localValue:2, serverValue:24}] to localhost:27017 because the pool has been closed.

0 个答案:

没有答案