【问题标题】:How do I update a single subelement in an array by two references如何通过两个引用更新数组中的单个子元素
【发布时间】:2015-08-06 13:49:10
【问题描述】:

我有一个这样的学校文档结构:

{
    "_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。

【问题讨论】:

    标签: mongodb meteor mongodb-query


    【解决方案1】:

    如下使用$elemMatch

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

    【讨论】:

    • 谢谢伙计。这工作得很好。我将阅读有关 $elemMatch 的更多信息。
    猜你喜欢
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    • 2018-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多