如何获取数据库中所有集合的列表?
答案 0 :(得分:15)
获取馆藏列表 每个数据库都有零个或多个集合。您可以从数据库中检索它们的列表(并打印出任何存在的数据):
Set<String> colls = db.getCollectionNames();
for (String s : colls) {
System.out.println(s);
}
编辑 :正如@ 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();
根据文档类型获取可迭代集合:
/**
* 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);
答案 1 :(得分:8)
在MongoDB 3中,它现在是db.listCollectionNames()
。还有db.listCollections()
请参阅API Docs。