
MongoDBDriverBulkWrite::insert 메서드를 사용할 때 발생하는 에러는, bulk write가 수행된 후에 해당 문서의 _id가 자동으로 생성되는 것을 방지하기 위해 발생합니다.
이러한 에러를 해결하기 위해서는, MongoDB의 insertOne 메서드를 사용하여 bulk write를 수행하는 방법을 사용할 수 있습니다.
insertOne 메서드는 bulk write를 수행할 때, _id를 직접 지정할 수 있습니다.
아래의 예제를 참고하여 insertOne 메서드를 사용하여 bulk write를 수행할 수 있습니다.
#hostingforum.kr
java
MongoDBCollection collection = db.getCollection("컬렉션명");
BulkWriteOptions options = new BulkWriteOptions().ordered(false);
BulkWriteOperation bulkWrite = collection.bulkWrite(options);
Document document = new Document("_id", ObjectId.get());
document.append("필드명", "값");
bulkWrite.insertOne(document);
또한, MongoDB 3.6 버전부터는 insertOne 메서드 대신 insertOneAsync 메서드를 사용할 수 있습니다.
#hostingforum.kr
java
MongoDBCollection collection = db.getCollection("컬렉션명");
BulkWriteOptions options = new BulkWriteOptions().ordered(false);
BulkWriteOperation bulkWrite = collection.bulkWrite(options);
Document document = new Document("_id", ObjectId.get());
document.append("필드명", "값");
bulkWrite.insertOneAsync(document).toFuture().get();
이러한 방법을 사용하면, bulk write를 수행할 때 _id를 직접 지정할 수 있습니다.
2025-07-05 06:59