java中mongo数据库中所有集合的列表 [英] List of all collections in mongo database in java

查看:103
本文介绍了java中mongo数据库中所有集合的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

how can I get a list of all the collections in the database?

  • 数据库 - mongodb;
  • 语言 - java;
  • ide - 日食;

推荐答案

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

Getting A List Of Collections Each database has zero or more collections. You can retrieve a list of them from the db (and print out any that are there) :

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

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

编辑:正如@Andrew 的回答所建议的,更新的 Java 客户端使用这个:

Edit : As suggested in @Andrew's answer, updated java client uses this :

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

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

and getting the iterable collection based on the document type :

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

这篇关于java中mongo数据库中所有集合的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆