java mongodb driver / mongo db driver / how to run command in java / mongodb.runCommand
mongodb runCommand 를 java 에서 실행하는 법
was 등의 application 에서 mongodb.runCommand 를 수행하고 싶을때를 위해 기록.
아래 예제는 java driver v4.3 의 예제이다.
package usage.examples;
import org.bson.BsonDocument;
import org.bson.BsonInt64;
import org.bson.Document;
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoException;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;
public class RunCommand {
public static void main(String[] args) {
// Replace the uri string with your MongoDB deployment's connection string
String uri = "<connection string uri>";
try (MongoClient mongoClient = MongoClients.create(uri)) {
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
try {
Bson command = new BsonDocument("dbStats", new BsonInt64(1));
Document commandResult = database.runCommand(command);
System.out.println("dbStats: " + commandResult.toJson());
} catch (MongoException me) {
System.err.println("An error occurred: " + me);
}
}
}
}
댓글 없음:
댓글 쓰기