
BsonDocument는 MongoDB의 문서를 나타내는 Java 객체입니다. executeCommand 함수의 파라미터로 사용되기 때문에, MongoDB에 명령을 실행할 때 사용됩니다. BsonDocument를 생성하는 방법은 다음과 같습니다.
1. BsonDocumentBuilder를 사용하여 BsonDocument를 생성할 수 있습니다. BsonDocumentBuilder는 BsonDocument를 생성하는 데 사용되는 빌더 클래스입니다.
#hostingforum.kr
java
BsonDocument document = new BsonDocumentBuilder()
.append("key1", "value1")
.append("key2", new BsonInt32(123))
.append("key3", new BsonDateTime(new Date()))
.build();
2. BsonDocument를 직접 생성할 수도 있습니다. BsonDocument는 키-값 쌍으로 구성되기 때문에, 키와 값을 직접 추가할 수 있습니다.
#hostingforum.kr
java
BsonDocument document = new BsonDocument();
document.append("key1", "value1");
document.append("key2", new BsonInt32(123));
document.append("key3", new BsonDateTime(new Date()));
3. BsonDocumentBuilder를 사용하여 BsonDocument를 생성하는 방법은 위에서 설명한 것과 같습니다. 하지만, BsonDocumentBuilder를 사용하여 BsonDocument를 생성하는 방법은 더 편리하고 간단합니다.
executeCommand 함수를 사용할 때 BsonDocument를 생성하는 방법은 위에서 설명한 것과 같습니다. BsonDocument를 생성한 후, executeCommand 함수에 BsonDocument를 전달하여 MongoDB에 명령을 실행할 수 있습니다.
#hostingforum.kr
java
MongoClient mongoClient = MongoClientSettings.builder()
.applyConnectionString(ConnectionString.builder()
.addHost("localhost:27017")
.build())
.build();
MongoDatabase database = mongoClient.getDatabase("mydatabase");
MongoCollection collection = database.getCollection("mycollection", BsonDocument.class);
BsonDocument document = new BsonDocumentBuilder()
.append("key1", "value1")
.append("key2", new BsonInt32(123))
.append("key3", new BsonDateTime(new Date()))
.build();
BsonDocument result = collection.executeCommand(document);
executeCommand 함수를 사용할 때 BsonDocument를 생성하는 방법은 위에서 설명한 것과 같습니다. BsonDocument를 생성한 후, executeCommand 함수에 BsonDocument를 전달하여 MongoDB에 명령을 실행할 수 있습니다.
2025-04-18 10:01