如何更新mongodb数组值 [英] How to update mongodb array values
本文介绍了如何更新mongodb数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像这样存储在 mongodb 中的文档
I have a document stored in mongodb like this
"Fiction" : [
{
"SNo": "1",
"authorName" : "Enid Blyton",
"bookName" : "Secret series",
"Amount" : 115
},
{
"SNo": "2",
"authorName" : "Enid Blyton",
"bookName" : "Mystery series",
"Amount" : 150
}
]
我想将金额从 115
更新为 135
.
I would like to update the amount from 115
to 135
.
我尝试像这样更新值.
db.fiction.update({"Fiction.SNo":"1"},{$set:{"Fiction.Amount":135}})
但我收到一个错误.
错误是
cannot use the part (Fiction of Fiction.Amount) to traverse the element
任何人都可以帮忙解决这个问题,我需要实现它才能从 python 中工作.
Can anyone help on this, I need to implement this to work from python.
推荐答案
使用 $ positional
操作符在您的更新中,因为它标识了要更新的数组中的元素,而无需明确指定其在数组中的位置:
Use the $ positional
operator in your update as this identifies the element in an array to update without explicitly specifying the its position in the array:
db.fiction.update({"Fiction.SNo":"1"},{$set:{"Fiction.$.Amount":135}})
这篇关于如何更新mongodb数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文