개발자 Q&A

개발하다 막혔다면? 여기서 질문하세요! 초보부터 고수까지, 함께 고민하고 해결하는 공간입니다. 누구나 자유롭게 질문하고 답변을 남겨보세요!

2025.04.19 10:47

MongoDBDriverManager::executeBulkWrite 사용 시 오류 발생 이유

목록
  • ORM수집가 3일 전 2025.04.19 10:47
  • 40
    1
제가 MySQL에서 MongoDB로 데이터베이스를 이전하여 MongoDBDriverManager를 사용하여 BulkWrite를 수행해 보고 있습니다. 그러나 다음과 같은 에러가 발생합니다.

```
Exception in thread "main" com.mongodb.MongoWriteException: Write failed with error code 11000 and detailed message 'duplicate key error collection: db.name document: _id : 12345',
at com.mongodb.client.internal.RequestHelper.rethrow(RequestHelper.java:73)
- 잘못된 키를 사용하여 BulkWrite를 수행함으로 인해 발생하는 오류로 생각합니다.

이 경우에는 BulkWrite를 사용하여 데이터를 삽입하는 과정에서, _id 필드에 중복된 값을 사용했을 때 발생하는 오류입니다.

이러한 오류를 피하기 위해서는 어떻게 해야 하나요?

위 에러는 BulkWrite를 사용할 때 _id 필드를 사용하여 문서를 식별할 때 발생할 수 있습니다.

이러한 문제를 해결하기 위해서는, _id 필드를 사용하지 않고 다른 필드를 사용하여 BulkWrite를 수행하거나, _id 필드를 고유하게 설정하거나, _bulkWriteOptions의 bypassDocumentValidation을 true로 설정하는 방법이 있습니다.

    댓글목록

    profile_image
    나우호스팅  3일 전



    이러한 오류를 피하기 위해서는 다음의 방법을 사용할 수 있습니다.

    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

  • 개발자 Q&A 포인트 정책
      글쓰기
      50P
      댓글
      10P
  • 전체 13,599건 / 62 페이지

검색

게시물 검색