如何通过两个引用更新数组中的单个子元素(How do I update a single subelement in an array by two references)
我有这样的学校文档结构:
{ "_id":"tnoMB2PhsPPQKcafz", // some other property here "students":[{ "student":{ "ref_id":"eaH7JsponapSWCGf6", "name":"name", "family":"family", "grade":12 }, // many other student here ]}我想在确定的学校更新确定学生的成绩属性,但我不知道该怎么做。
例如,我有学校_id( tnoMB2PhsPPQKcafz )和学生ref_id( eaH7JsponapSWCGf6 ),并希望将确定的学生成绩从12更新到18。
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.
最满意答案
使用$elemMatch如下:
db.school.update({"_id" : "tnoMB2PhsPPQKcafz","students":{"$elemMatch":{"student.ref_id":"eaH7JsponapSWCGf6"}}},{"$set":{"students.$.student.grade":18}})Use $elemMatch as below :
db.school.update({"_id" : "tnoMB2PhsPPQKcafz","students":{"$elemMatch":{"student.ref_id":"eaH7JsponapSWCGf6"}}},{"$set":{"students.$.student.grade":18}})更多推荐
发布评论