java中mongo数据库中所有集合的列表

Sas*_*ska 5 java mongodb mongodb-java

如何获取数据库中所有集合的列表?

  • 数据库 - mongodb;
  • language - java;
  • ide - eclipse;

reg*_*gie 15

获取集合列表每个数据库都有零个或多个集合.您可以从数据库中检索它们的列表(并打印出任何存在的数据):

Set<String> colls = db.getCollectionNames();

for (String s : colls) {
System.out.println(s);
}
Run Code Online (Sandbox Code Playgroud)

编辑:正如@ Andrew的答案中所建议的,更新的java客户端使用此:

/**
 * Gets the names of all the collections in this database.
 *
 * @return an iterable containing all the names of all the collections in this database
 */
MongoIterable<String> listCollectionNames();
Run Code Online (Sandbox Code Playgroud)

并根据文档类型获取可迭代集合:

/**
 * Finds all the collections in this database.
 *
 * @param resultClass the class to decode each document into
 * @param <TResult>   the target document type of the iterable.
 * @return the list collections iterable interface
 * @mongodb.driver.manual reference/command/listCollections listCollections
 */
<TResult> ListCollectionsIterable<TResult> listCollections(Class<TResult> resultClass);
Run Code Online (Sandbox Code Playgroud)


And*_*rew 8

在MongoDB 3中,现在是db.listCollectionNames().还有db.listCollections()

请参阅API文档.