获取“错误”:“使用 Java 驱动程序插入 mongo 时出现 E11000 重复密钥错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21119928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:44:07  来源:igfitidea点击:

Getting "err" : "E11000 duplicate key error when inserting into mongo using the Java driver

javamongodbtwitter4j

提问by Nautilus_o

Exception in thread "main" com.mongodb.MongoException$DuplicateKey: { "serverUsed" : "localhost/127.0.0.1:27017" , "err" : "E11000 duplicate key error index: twitterdb03.LevelAFollowers.$iddup key: { : ObjectId('52d5636de408652b4853a8fe') }" , "code" : 11000 , "n" : 0 , "connectionId" : 12 , "ok" : 1.0}

线程“main”com.mongodb.MongoException$DuplicateKey 中的异常:{“serverUsed”:“localhost/127.0.0.1:27017”,“err”:“E11000 重复键错误索引:twitterdb03.LevelAFollowers.$ iddup 键:{ : ObjectId('52d5636de408652b4853a8fe') }" , "code" : 11000 , "n" : 0 , "connectionId" : 12 , "ok" : 1.0}

I'm using mongo 2.11.1

我正在使用 mongo 2.11.1

Never had problems with simple write operations in java

java中的简单写操作从来没有问题

myMap.put(inid, followersList);
myObj.putAll(myMap);
myIdMapCollection.insert(myObj);

回答by Ori Dar

Try calling myIdMapCollection.save(myObj);instead of myIdMapCollection.insert(myObj);

尝试调用myIdMapCollection.save(myObj);而不是myIdMapCollection.insert(myObj);

The savemethod, unlike insertdoes upsert, meaning if a document contains _id, it replaces that document.

save方法与insertupsert不同,这意味着如果文档包含_id,则替换该文档。

My guess is that you had fetched the DBObjectusing a cursor | query, had manipulated it, and you want to persist the changes. In that case, saveis the right way to do it.

我的猜测是您已经DBObject使用游标获取了| 查询,已经对其进行了操作,并且您希望保留更改。在这种情况下,save是正确的方法。

So, when calling insertthe DBObjectis already associated with _id, calling insertthus fails, because you already have a document with that _idin the collection, which should be unique (duplicate index error).

所以,当调用insertDBObject已经与相关的_id,称insert这样失败了,因为你已经有一个文档_id集合中,这应该是唯一的(重复的索引错误)。

回答by Florian

I found an answer on this page. I'm guessing your code looks something like this (greatly simplified)?:

在这个页面上找到了答案。我猜你的代码看起来像这样(大大简化了)?:

doc = {} 
for i in xrange(2): 
    doc['i'] = i 
    collection.insert(doc) 

The problem is that PyMongo injects an _id field into the document, if the _idfield does not exist, before inserting it (_idis always generated client side with 10gen drivers). That means that the first time through the loop _idis added by the insert method. Since docis defined outside the loop, each subsequent pass through the loop uses the samevalue for _id.

问题是 PyMongo 会在文档中注入一个 _id 字段,如果该_id字段不存在,则在插入它之前(_id总是使用 10gen 驱动程序生成客户端)。这意味着第一次通过循环_id是由插入方法添加的。由于doc是在循环外定义的,因此每次后续通过循环都使用相同的_id

Solution:

解决方案:

  1. Delete the key _id
  1. 删除键_id
for i in xrange(2): 
    doc['i'] = i 
    if '_id' in doc: 
        del doc['_id'] 
    collection.insert(doc)
for i in xrange(2): 
    doc['i'] = i 
    if '_id' in doc: 
        del doc['_id'] 
    collection.insert(doc)
  1. Or create manually a new one:
  1. 或者手动创建一个新的:
from bson.objectid import ObjectId 
for i in xrange(2): 
    doc['i'] = i 
    doc['_id'] = ObjectId() 
    collection.insert(doc)
from bson.objectid import ObjectId 
for i in xrange(2): 
    doc['i'] = i 
    doc['_id'] = ObjectId() 
    collection.insert(doc)