我尝试更新嵌入式文档字段中的数组。但是如何实现这种方案是行不通的。
数据集
{
"id":101,
"review_ratings":[
{
id:102,
name:"hari"
},{
id:103,
name:"mani"
}
]
}
如何更新id 103名称字段
答案 0 :(得分:0)
您应该使用位置运算符
db.test.update({"id":101, "review_ratings.id": 103},
{$set: {"review_ratings.$.name":"mohit"}});
答案 1 :(得分:0)
You can use aggregate function for searching and can use find and update method like:-
test.aggregate(
[
{ $match: {} },
{$project: {
review_ratings: {
$filter: {
input: '$review_ratings',
as: 'review_ratings',
cond: { $wq: [ '$$review_ratings.id', '103' ] }
}
}
}
}
],
(err, res) => {
});