如何获取数据库中所有集合的列表?
- 数据库——mongodb;
- 语言——java;
- ide - 日食;
获取集合列表
每个数据库都有零个或多个集合。您可以从数据库中检索它们的列表(并打印出其中的任何内容):
Set<String> colls = db.getCollectionNames();
for (String s : colls) {
System.out.println(s);
}
Edit:正如@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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)