
이러한 오류를 피하기 위해서는 다음의 방법을 사용할 수 있습니다.
1. _id 필드를 사용하지 않고 다른 필드를 사용하여 BulkWrite를 수행합니다. 예를 들어, 다음과 같이 사용할 수 있습니다.
#hostingforum.kr
java
MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017");
MongoDatabase mongoDatabase = mongoClient.getDatabase("db");
MongoCollection collection = mongoDatabase.getCollection("collection");
Bson filter = Filters.eq("name", "John");
Bson update = Updates.set("age", 30);
BulkWriteOptions bulkWriteOptions = new BulkWriteOptions().bypassDocumentValidation(true);
List> writes = Arrays.asList(
new UpdateOneModel<>(filter, update, new Document("_id", 1))
);
collection.bulkWrite(writes, bulkWriteOptions);
2. _id 필드를 고유하게 설정합니다. 예를 들어, 다음과 같이 사용할 수 있습니다.
#hostingforum.kr
java
MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017");
MongoDatabase mongoDatabase = mongoClient.getDatabase("db");
MongoCollection collection = mongoDatabase.getCollection("collection");
Bson filter = Filters.eq("name", "John");
Bson update = Updates.set("age", 30);
List> writes = Arrays.asList(
new UpdateOneModel<>(filter, update, new Document("_id", 1))
);
collection.insertMany(writes);
3. _bulkWriteOptions의 bypassDocumentValidation을 true로 설정합니다. 예를 들어, 다음과 같이 사용할 수 있습니다.
#hostingforum.kr
java
MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017");
MongoDatabase mongoDatabase = mongoClient.getDatabase("db");
MongoCollection collection = mongoDatabase.getCollection("collection");
Bson filter = Filters.eq("name", "John");
Bson update = Updates.set("age", 30);
BulkWriteOptions bulkWriteOptions = new BulkWriteOptions().bypassDocumentValidation(true);
List> writes = Arrays.asList(
new UpdateOneModel<>(filter, update, new Document("_id", 1))
);
collection.bulkWrite(writes, bulkWriteOptions);
이러한 방법을 사용하여 BulkWrite를 수행할 때 _id 필드에 중복된 값을 사용하는 오류를 피할 수 있습니다.
2025-04-19 10:48