
MongoDBDriverBulkWrite::count 오류는 BulkWrite 작업의 카운트 값을 가져올 때 발생하는 오류입니다.
이러한 문제가 발생하는 원인은 BulkWrite 작업의 결과에 포함되지 않은 수정된 문서의 수를 가져오려고 할 때 발생합니다.
해결 방법은 BulkWrite 작업의 결과에 포함되지 않은 수정된 문서의 수를 가져오려고 할 때, getModifiedCount() 대신 getUpsertedCount() 또는 getInsertedCount() 메서드를 사용하는 것입니다.
또한, BulkWrite 작업의 결과에 포함되지 않은 수정된 문서의 수를 가져오려고 할 때, getModifiedCount() 메서드를 사용하는 대신, MongoDB의 aggregation framework를 사용하는 방법도 있습니다.
아래는 aggregation framework를 사용하는 방법의 예제입니다.
#hostingforum.kr
java
List> writes = new ArrayList<>();
writes.add(new UpdateOneModel<>(Filters.eq("id", 1), new Document("$set", new Document("name", "John"))));
BulkWriteResult result = collection.bulkWrite(writes, options);
List pipeline = new ArrayList<>();
pipeline.add(new Document("$match", new Document("modifiedCount", new Document("$gt", 0))));
pipeline.add(new Document("$group", new Document("_id", null).append("count", new Document("$sum", "$modifiedCount"))));
List resultDocs = collection.aggregate(pipeline).into(new ArrayList<>());
long count = resultDocs.get(0).getLong("count");
이러한 방법을 사용하면 BulkWrite 작업의 결과에 포함되지 않은 수정된 문서의 수를 정확하게 가져올 수 있습니다.
2025-06-27 06:00