获取“错误”:“使用 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
Getting "err" : "E11000 duplicate key error when inserting into mongo using the Java driver
提问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 save
method, unlike insert
does upsert, meaning if a document contains _id
, it replaces that document.
该save
方法与insert
upsert不同,这意味着如果文档包含_id
,则替换该文档。
My guess is that you had fetched the DBObject
using a cursor | query, had manipulated it, and you want to persist the changes. In that case, save
is the right way to do it.
我的猜测是您已经DBObject
使用游标获取了| 查询,已经对其进行了操作,并且您希望保留更改。在这种情况下,save
是正确的方法。
So, when calling insert
the DBObject
is already associated with _id
, calling insert
thus fails, because you already have a document with that _id
in the collection, which should be unique (duplicate index error).
所以,当调用insert
了DBObject
已经与相关的_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 _id
field does not exist, before inserting it (_id
is always generated client side with 10gen drivers). That means that the first time through the loop _id
is added by the insert method. Since doc
is defined outside the loop, each subsequent pass through the loop uses the samevalue for _id
.
问题是 PyMongo 会在文档中注入一个 _id 字段,如果该_id
字段不存在,则在插入它之前(_id
总是使用 10gen 驱动程序生成客户端)。这意味着第一次通过循环_id
是由插入方法添加的。由于doc
是在循环外定义的,因此每次后续通过循环都使用相同的值_id
。
Solution:
解决方案:
- Delete the key _id
- 删除键_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)
- Or create manually a new one:
- 或者手动创建一个新的:
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)