Mongodb更新抛出DuplicateKeyException [英] Mongodb upsert throwing DuplicateKeyException

查看:1421
本文介绍了Mongodb更新抛出DuplicateKeyException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试使用方法[1]向上插入(递增或插入)文档时,确实会偶尔出现此错误[2].

I do get this error [2] from time to time when attempting to upsert (increment or insert) a document using method [1].

[1]

public NGram save(final NGram ngram) {
    Criteria cr = where("_id").is(ngram.getNgram())
            .and("f3c").is(ngram.getF3c())
            .and("tokCount").is(ngram.getTokCount())
            .and("first").is(ngram.getFirst())
            ;
    if( ngram.getTokCount() > 1 ) {
        cr.and("second").is(ngram.getSecond());
    }
    if( ngram.getTokCount() > 2 ) {
        cr.and("third").is(ngram.getThird());
    }
    final Query qry = new Query( cr );
    final Update updt = new Update().inc("count", ngram.getCount());
    template.upsert(qry, updt, NGram.class);
    return ngram;
}

[2]

Caused by: org.springframework.dao.DuplicateKeyException: E11000 duplicate key error index: sytrue.ngram.$_id_  dup key: { : "page two" }; nested exception is com.mongodb.MongoException$DuplicateKey: E11000 duplicate key error index: sytrue.ngram.$_id_  dup key: { : "page two" }
at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:52)
at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:1665)
at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:390)
at org.springframework.data.mongodb.core.MongoTemplate.doUpdate(MongoTemplate.java:920)
at org.springframework.data.mongodb.core.MongoTemplate.upsert(MongoTemplate.java:894)
at com.sytrue.ngram.repo.impl.NGramRepositoryImpl.save(NGramRepositoryImpl.java:43)
at sun.reflect.GeneratedMethodAccessor41.invoke(Unknown Source)

Upsert永远不会返回此异常.我说的对吗?

Upsert should never return me this exception. Am I right?

推荐答案

我正在猜测的问题可能是:

The issue that I am just guessing might be following :

您正在基于许多条件进行查找操作.这意味着,如果由于某个参数不匹配而失败(在条件中),它将尝试插入文档.

You are doing find operations based on many criteria. That means if it fails because of any mismatch of a param ( in criteria ) then it will try to insert the document.

因此,您有机会尝试使用相同的_id更新同一文档,但其他一些条件不匹配,导致再次插入该文档,这将导致重复的键异常.考虑下面的例子

So, chances are there, that you are trying to update the same document with same _id but some of the other criteria is not matching, causing it to insert again which will cause duplicate key exception. Consider the below example

test:Mongo > db.example.update({ _id : 1, a : 1, b : 1},{ $set : {d : 1}}, true, false)
test:Mongo > db.example.find()
{ "_id" : 1, "a" : 1, "b" : 1, "d" : 1 }
test:Mongo > db.example.update({ _id : 1, a : 1, b : 2},{ $set : {d : 1}}, true, false)
E11000 duplicate key error index: test.example.$_id_  dup key: { : 1.0 }

这篇关于Mongodb更新抛出DuplicateKeyException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆