[英]List of all collections in mongo database in java
如何獲取數據庫中所有集合的列表?
獲取集合列表 每個數據庫都有零個或多個集合。 您可以從數據庫中檢索它們的列表(並打印出其中的任何內容):
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);
在 MongoDB 3 中,它現在是db.listCollectionNames()
。 還有db.listCollections()
請參閱API 文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.