Список всех коллекций в базе данных Монго в Java

StackOverflow https://stackoverflow.com/questions/4971329

  •  12-11-2019
  •  | 
  •  

Вопрос

как я могу получить список всех коллекций в базе данных?

  • база данных — монгодб;
  • язык - Java;
  • язь – затмение;
Это было полезно?

Решение

Получение списка коллекций Каждая база данных имеет ноль или более коллекций.Вы можете получить список из них из БД (и распечатать любые, которые там есть):

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

for (String s : colls) {
System.out.println(s);
}
.

Редактировать : Как предложено в @ Andrew's ответ, обновленный Java Client использует это:

/**
 * 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();
.

и получение коллекции Iterable на основе типа документа:

/**
 * 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 с атрибуция
Не связан с StackOverflow
scroll top