如何使用 java 获取所有 MongoDB 数据库的列表?
在 MongoDB 中,你可以使用 show dbs 命令来查看数据库列表
> show dbs admin config local myDatabase sampleDatabase students test testDB
在 Java 中,你可以使用 getDatabaseNames() 方法获取 MongoDB 中的所有数据库列表。
示例
import com.mongodb.client.MongoIterable;
import com.mongodb.MongoClient;
public class ListOfDatabases {
public static void main( String args[] ) {
// Creating a Mongo client
MongoClient mongo = new MongoClient( "localhost" , 27017 );
//Retrieving the list of collections
MongoIterable<String> list = mongo.listDatabaseNames();
for (String name : list) {
System.out.println(name);
}
}
}输出
admin config local myDatabase sampleDatabase students test testDB
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP