如何通过两个引用更新数组中的单个子元素(How do I update a single subelement in an array by two references)

编程入门 行业动态 更新时间:2024-10-23 23:24:31
如何通过两个引用更新数组中的单个子元素(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}})

更多推荐

本文发布于:2023-07-19 09:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1176928.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:个子   组中   元素   两个   update

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!