给定一个mongodb数据
{
"_id" : ObjectId("552f283dd951e49c6f2f451d"),
"uuid" : "1-2-1-b95a4040-e29d-11e4-bce8-0381ce4bc8a5",
"sub" : [
{
"prod" : 30,
"var" : 0,
"status" : "Test",
"files" : [
{
"filePath" : "20150415/2-1/21001429153881552f2859699769.82145796.jpg"
},
{
"filePath" : "20150415/2-1/21001429153880552f28589ca9a8.67013013.jpg"
}
]
},
{
"prod" : 10,
"var" : 0,
"status" : "Pending",
"files" : []
}
],
"process_marker" : 3
}
我想更新“sub.status”的状态,其中“uuid”:“1-2-1-b95a4040-e29d-11e4-bce8-0381ce4bc8a5”、“sub.prod”:10、“sub.prod”: 0
通常我们会使用“$”来修改结果索引,如下所示:
use targetDB
db.collection.update({
"uuid" : "1-2-1-b95a4040-e29d-11e4-bce8-0381ce4bc8a5",
"sub.prod":10,
"sub.var":0
},{
"$set":{"sub.$.status":"MyNewValue"}
})
==== BUT THE CODE ABOVE DOES NOT UPDATE THE CORRECT $ TARGET ====
它更新了 "prod":30, "var":0 集...这是为什么呢?
如果我们限制两个键值对的查询条件,如下所示,正确的数组集被更新
use targetDB
db.collection.update({
"uuid" : "1-2-1-b95a4040-e29d-11e4-bce8-0381ce4bc8a5",
"sub.prod":10
},{
"$set":{"sub.$.status":"MyNewValue"}
})
==== THE CODE ABOVE UPDATES THE CORRECT $ TARGET ====
我很困惑,更详细的查找查询会导致更新错误的数组集。这是一个错误还是我做错了什么?
MongoDB 版本:3.0.2