Javaを使用してMongoDBにデータベースをドロップする方法は?
db.dropDatabase()
Javaでデータベースを削除するには、まず、 getDatabase()を使用して必要なデータベースのオブジェクトを取得します。 メソッドを作成し、drop()メソッドを呼び出して削除します。
import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;
public class DropingDatabase {
public static void main( String args[] ) {
//Creating a MongoDB client
@SuppressWarnings("resource")
MongoClient mongo = new MongoClient( "localhost" , 27017 );
//Creating a database
mongo.getDatabase("myDatabase");
System.out.println("Databases created successfully");
MongoDatabase database = mongo.getDatabase("myDatabase");
database.drop();
}
} 出力
Database deleted successfully
-
Javaを使用してすべてのMongoDBデータベースのリストを取得するにはどうすればよいですか?
MongoDBでは、showdbsコマンドを使用してデータベースのリストを表示できます > 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 {
-
Javaを使用してMongoDBデータベース内のすべてのコレクションを一覧表示するにはどうすればよいですか?
show collectionsを使用して、データベース内の既存のすべてのコレクションのリストを印刷できます。 例 以下に示すように、MongoDBデータベースに3つのコレクションを作成したと仮定します- > use sampleDatabase switched to db sampleDatabase > db.createCollection("students") { "ok" : 1 } > db.createCollection("teachers") { "ok" : 1 }