【发布时间】:2014-04-28 12:30:40
【问题描述】:
看来您(/I )不能同时拥有 upsert 和 array element update operation。
如果你这样做(python):
findDct = {
"_id": ObjectId("535e3ab9c36b4417d031402f"),
'events.ids': '176976332'
}
print col.update(findDct, {"$set" : {"events.$.foo": "bar"} }, upsert=True)
它会抛出:
pymongo.errors.DuplicateKeyError: insertDocument :: caused by :: 11000 E11000
duplicate key error index: test.col.$_id_ dup key: { : ObjectId('535e3ab9c36b4417d031402f') }
发生这种情况是因为“_id”当然是一个索引,并且 mongo 尝试将文档作为新文档插入,因为 find 查询在其 'events.ids': '176976332' 部分 (cheat) 上失败。
是否可以使用 upsert True/如何更新数组中的未知元素?
【问题讨论】:
标签: mongodb mongodb-query