我正在尝试更新其中包含空值字段的现有文档,并且收到以下错误。
文件:
{
"_id" : ObjectId("582299f71e21dbf65027325e"),
"b" : "5555",
"f" : null
}
查询:
db.getCollection('temp').update({"b":"5555"},{"$set":{"f.1.b":1,"f.2.b":2}})
错误:
WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 16837,
"errmsg" : "cannot use the part (f of f.1.b) to traverse the element ({f: null})"
}
})
任何人都可以告诉我它为什么没有更新文档中的值。
谢谢。
答案 0 :(得分:2)
因为找不到要设置的f.1.b and f.2.b
对象
在这种情况下,你可以试试这个
准备一个适当的对象,并尝试设置
例如:
var temp = {
1 : {b: 1},
2: {b : 2}
}
db.getCollection('temp').update({"b":"5555"},{"$set":{f:temp}})