繁体   English   中英

更新MongoDB中的第二级数组元素

[英]Update 2nd level array element in MongoDB

在以下mongo集合中,如何将学生1的活动字段更新为true (鉴于这一点,我们知道hostel_name,room_no,student_id要更新)

{
  "hostel_name": "ABC",
  "rooms": [
    {
      "room_no": 1,
      "students": [
        {
          "student_id": 1,
          "active": false
        },
        {
          "student_id": 2,
          "active": true
        }
      ]
    }
  ]
}

回答:

db.collectionname.update({"rooms.students.student_id" : 1},{"$set" : {"rooms.$.students.0.active" : true}})

{
    "_id" : ObjectId("5564fb68c8fc4ec0f15c6dd4"),
    "hostel_name" : "ABC",
    "rooms" : [ 
        {
            "room_no" : 1,
            "students" : [ 
                {
                    "student_id" : 1,
                    "active" : true
                }, 
                {
                    "student_id" : 2,
                    "active" : true
                }
            ]
        }
    ]
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM