spring data mongorepository.save E11000有时会出现重复键错误索引

时间:2018-06-04 15:46:22

标签: java mongodb spring-boot spring-data-jpa spring-data-mongodb

我使用jhipster创建了应用程序,我使用MongoRepository来插入或保存文档。但它有时会抛出异常,但无法弄清楚可能的原因是什么。我在生产中遇到以下异常,即ubuntu:

nested exception is com.mongodb.DuplicateKeyException: Write failed with 
error code 11000 and error message 'E11000 duplicate key error index: 
db_test.collection_test.$_id_ du
p key: { : "1212" }'
    at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:73)
    at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2114)
    at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:464)
    at org.springframework.data.mongodb.core.MongoTemplate.saveDBObject(MongoTemplate.java:1080)
    at org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:1015)
    at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:961)
    at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.save(SimpleMongoRepository.java:80)

以下是java代码:

@JsonIgnoreProperties(ignoreUnknown = true)
@Document(collection = "collection_test")
public class CollectionTest implements Serializable {

@Id
private String id;

@Field("c_date")
@Indexed
private String cDate;

public CollctionTest() {
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getCDate() {
    return cDate;
}

public void setCDate(String cDate) {
    this.cDate = cDate;
}

@Override
public String toString() {
    return "CollctionTest{" +
            "id='" + id + '\'' +
            ", cDate='" + cDate + '\'' +
            '}';
}

}

public interface CollectionTestRepository extends 
 MongoRepository<CollectionTest, String> {
}

@Component
public class CollectionTestService implements ICollectionTestService {

@Autowired
private CollectionTestRepository collectionTestRepository ;

public void saveOrUpdate(CollectionTest collectionTest ) {
    try {
        collectionTestRepository.save(collectionTest);
        LOGGER.info("Inserted document with id: " + collectionTest .getId() 
        + " into database");

    } catch (Exception ex) {
        LOGGER.error("Exception during save or update", ex);
    }
}

}

注意:此应用程序通过rabbitmq侦听器使用集合测试对象并调用saveOrUpdate()方法。

_id具有我们生成的值中的自定义值。如果db中已存在_id,则只需更新文档,否则插入新文档。使用MongoRepository.save()用于相同目的

1 个答案:

答案 0 :(得分:0)

this particular exception occurs from DB when you have a unique index in your DB and try to put duplicate data field.

First, you check your current unique indexes :

db.collectionName.getIndexes()

you try to update doc on specific id and putting duplicate data for your other indexing fields, in this case, DB throws an exception for the _id field index. although it's the wrong exception, it should show for a specific duplicate index name field but because you are doing update operation it shows on _id

in your coding, always remember that if you are using any unique index then before call saves first to get the particular collection on unique index keys, and then call update.