Mongodb 如何通过两个引用更新数组中的单个子元素

Mongodb 如何通过两个引用更新数组中的单个子元素,mongodb,meteor,mongodb-query,Mongodb,Meteor,Mongodb Query,我的学校文件结构如下: { "_id":"tnoMB2PhsPPQKcafz", // some other property here "students":[{ "student":{ "ref_id":"eaH7JsponapSWCGf6", "name":"name", "family":"family", "grade":12 },

我的学校文件结构如下:

{
    "_id":"tnoMB2PhsPPQKcafz",
    // some other property here
    "students":[{
        "student":{
            "ref_id":"eaH7JsponapSWCGf6",
            "name":"name",
            "family":"family",
            "grade":12
        },
        // many other student here
]}
我想更新已确定学校中已确定学生的成绩属性,但我不知道如何执行此操作


例如,我有学校id(tnoMB2PhsPPQKcafz)和学生参考id(eaH7JsponapSWCGf6),希望将确定的学生成绩从12分更新到18分。

使用
$elemMatch
,如下所示:

db.school.update({"_id" : "tnoMB2PhsPPQKcafz","students":{"$elemMatch":{"student.ref_id":"eaH7JsponapSWCGf6"}}},{"$set":{"students.$.student.grade":18}})

使用
$elemMatch
如下:

db.school.update({"_id" : "tnoMB2PhsPPQKcafz","students":{"$elemMatch":{"student.ref_id":"eaH7JsponapSWCGf6"}}},{"$set":{"students.$.student.grade":18}})

谢谢你,伙计。这是很好的工作。我将阅读更多关于$elemMatch的内容。谢谢你。这是很好的工作。我将阅读更多关于$elemMatch的内容。