这与我之前的问题How to update mongodb array values
有些相似"Fiction" : [
{
"SNo": "1",
"authorName" : "Enid Blyton",
"bookSeries" : "Secret Seven series",
"bookName" : [{"Title": "Secret Seven Adeventure", "Amount": 115},
{"Title": "Well done Secret Seven", "Amount": 135}]
},
{
"SNo": "2",
"authorName" : "Enid Blyton",
"bookSeries" : "Mystery series",
"bookName" : [{"Title": "Burnt Cottage", "Amount": 150},
{"Title": "Vanished Prince", "Amount": 140}]
}
]
我希望将Amount: 115
更新为Amount: 135
。
从解决方案到早期的问题,我通过做这样的事情来尝试它
db.Fiction.update({"Fiction.SNo": "1","Fiction.bookName.$.Title": "Secret Seven Adeventure"},{$set:{"Fiction.bookName.$.Amount": 135}})
但我得到了WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
任何人都可以告诉我为什么会这样吗
修改
建议的答案仅在数组的索引已知时才有效。
但在我的情况下,数组的index
是未知的。