我有问题导入数据到mongodb与。我有一个test.json
文件是这样的:mongoimport不符合“_id字段不能改变”
{"_id":{"s":{"$numberLong":"38851448"},"a":5},"someKey":"someValue"}
{"_id":{"s":{"$numberLong":"38853194"},"a":5},"someKey":"someValue"}
{"_id":{"s":{"$numberLong":"38760498"},"a":5},"someKey":"someValue"}
{"_id":{"s":{"$numberLong":"39099662"},"a":5},"someKey":"someValue"}
{"_id":{"s":{"$numberLong":"38855558"},"a":5},"someKey":"someValue"}
{"_id":{"s":{"$numberLong":"38760487"},"a":5},"someKey":"someValue"}
{"_id":{"s":{"$numberLong":"38760488"},"a":5},"someKey":"someValue"}
{"_id":{"s":{"$numberLong":"39099663"},"a":5},"someKey":"someValue"}
{"_id":{"s":{"$numberLong":"38851450"},"a":5},"someKey":"someValue"}
{"_id":{"s":{"$numberLong":"38853546"},"a":5},"someKey":"someValue"}
我尝试使用以下命令将其导入:
mongoimport --type json --db test --collection coll --file test.json --upsert
进口几乎总是失败,出现相同的错误消息:
2015-08-27T17:02:15.510+0200 error inserting documents: The _id field cannot be changed from {_id: { s: 38851448, a: 5 }} to {_id: { a: 5, s: 38851448 }}.
2015-08-27T17:02:15.511+0200 error inserting documents: The _id field cannot be changed from {_id: { a: 5, s: 38760487 }} to {_id: { s: 38760487, a: 5 }}.
这是令人沮丧的是,这个错误甚至不可重现。进口商似乎改变了_id
的属性顺序,但我不知道为什么,这是一个应该报告或已知的BUG?或者有没有 有问题,我没有看到。
我甚至试图改变s
和a
在_id
的顺序,但问题保持不变。
如果我索引树跑具有完全相同的数据文件中多次出现错误的文件发生变化,有一段时间,进口的所有行如预期完全相同的导入命令,但只是一个时间:
为了完整:我在Mac OS X 10.10.5上使用了mongo 3.0.5,并且安装了homebrew。
UPDATE: 我创建了一个票务与MongoDB的特攻队:TOOLS-894
UPDATE2:
我尽量不使用_id但ID为我的唯一关键:
{"id":{"s":{"$numberLong":"38851448"},"a":5},"someKey":"someValue"}
{"id":{"s":{"$numberLong":"38853194"},"a":5},"someKey":"someValue"}
{"id":{"s":{"$numberLong":"38760498"},"a":5},"someKey":"someValue"}
{"id":{"s":{"$numberLong":"39099662"},"a":5},"someKey":"someValue"}
{"id":{"s":{"$numberLong":"38855558"},"a":5},"someKey":"someValue"}
{"id":{"s":{"$numberLong":"38760487"},"a":5},"someKey":"someValue"}
{"id":{"s":{"$numberLong":"38760488"},"a":5},"someKey":"someValue"}
{"id":{"s":{"$numberLong":"39099663"},"a":5},"someKey":"someValue"}
{"id":{"s":{"$numberLong":"38851450"},"a":5},"someKey":"someValue"}
{"id":{"s":{"$numberLong":"38853546"},"a":5},"someKey":"someValue"}
并将其与此导入:
mongoimport --type json --db test --collection coll --file test.json --upsertFields id
现在我没有得到任何错误,但在两次导入后,我在集合中有15行而不是10个。再次因为id
中的属性的顺序。
好像'--upsert'标志导致了这个错误,是否需要你的情况。似乎mongo在尝试使用组合标识更新数据时会导致问题。 – cubbuk
是的upsert是必要的,因为我得到的JSON文件是从另一个(非MongoDB)系统转储并包含新的和更新的文档,所以upsert是强制性的,因为如果没有它,它将无法用'E11000重复密钥错误收集' – jigfox