如何使用 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