[英]How do I update a single subelement in an array by two references
I have a school document structure like this: 我有一个像这样的学校文件结构:
{
"_id":"tnoMB2PhsPPQKcafz",
// some other property here
"students":[{
"student":{
"ref_id":"eaH7JsponapSWCGf6",
"name":"name",
"family":"family",
"grade":12
},
// many other student here
]}
I want to update grade property for determined student in determined school but I don't know how to do this. 我想更新确定学校中确定学生的年级属性,但是我不知道该怎么做。
For example I have school _id ( tnoMB2PhsPPQKcafz ) and student ref_id ( eaH7JsponapSWCGf6 ) and want to update determined student grade from 12 to 18. 例如,我有学校_id( tnoMB2PhsPPQKcafz )和学生ref_id( eaH7JsponapSWCGf6 ),并且想要将确定的学生成绩从12更新为18。
使用$elemMatch
如下:
db.school.update({"_id" : "tnoMB2PhsPPQKcafz","students":{"$elemMatch":{"student.ref_id":"eaH7JsponapSWCGf6"}}},{"$set":{"students.$.student.grade":18}})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.