MongoDB更新内嵌list中某个字段的值
作者:互联网
更新内嵌的list中某个字段的值,主要使用了arrayFilters参数
插入数据
db.students2.insertMany([
{
"_id" : 1,
"grades" : [
{ "grade" : 80, "mean" : 75, "std" : 6 },
{ "grade" : 85, "mean" : 90, "std" : 4 },
{ "grade" : 85, "mean" : 85, "std" : 6 }
]
}
{
"_id" : 2,
"grades" : [
{ "grade" : 90, "mean" : 75, "std" : 6 },
{ "grade" : 87, "mean" : 90, "std" : 3 },
{ "grade" : 85, "mean" : 85, "std" : 4 }
]
}
])
更新
db.students2.update(
{ },
{ $set: { "grades.$[elem].mean" : 100 } },
{
multi: true,
arrayFilters: [ { "elem.grade": { $gte: 85 } } ]
}
)
标签:std,grade,MongoDB,list,grades,字段,90,mean,85 来源: https://www.cnblogs.com/lovehunterYjj/p/14799564.html