更新嵌套数组文档中的一个字段

时间:2020-07-15 16:15:06

标签: mongodb

假设我有一个这样的架构:

{ 
    "_id" : ObjectId("5f0c64e4dd0a36b93c7deafa"), 
    "name" : "Asd", 
    "email" : "asd@asd.com", 
    "created_at" : ISODate("2020-07-13T20:43:00.871+0000"), 
    "updated_at" : ISODate("2020-07-13T23:08:26.149+0000"), 
    "family_members" : [
        
    ], 
    "rooms" : [
        {
            "_id" : ObjectId("5f0c98826f0321f6986755da"), 
            "name" : "Ruang Makan", 
            "created_at" : ISODate("2020-07-14T00:23:14.839+0000"), 
            "updated_at" : ISODate("2020-07-14T00:23:14.840+0000"), 
            "devices" : [

            ]
        }, 
        {
            "_id" : ObjectId("5f0c98876f0321f6986755dd"), 
            "name" : "Ruang Tamu", 
            "created_at" : ISODate("2020-07-14T00:23:19.693+0000"), 
            "updated_at" : ISODate("2020-07-14T19:00:08.281+0000"), 
            "devices" : [
                {
                    "serial_number" : "ST9L0CY4A2AVY7HFWWUE", 
                    "used_relay" : NumberInt(0), 
                    "created_at" : ISODate("2020-07-14T16:56:22.156+0000"), 
                    "updated_at" : ISODate("2020-07-14T16:56:22.156+0000"), 
                    "sensors" : [
                       {
                            "_id" : ObjectId("5f0db478ca203bde99d2438e"), 
                            "name" : "Temperature", 
                            "value" : null, 
                            "created_at" : ISODate("2020-07-14T20:34:48.134+0000"), 
                            "updated_at" : ISODate("2020-07-14T20:34:48.134+0000")
                        }, 
                        {
                            "_id" : ObjectId("5f0dbe0563ccbcb2a2aecc04"), 
                            "name" : "Motion", 
                            "value" : null, 
                            "created_at" : ISODate("2020-07-14T21:15:33.135+0000"), 
                            "updated_at" : ISODate("2020-07-14T21:15:33.135+0000")
                        }, 
                        {
                            "_id" : ObjectId("5f0dc0412022e93af338316f"), 
                            "name" : "Humidity", 
                            "value" : null, 
                            "created_at" : ISODate("2020-07-14T21:25:05.126+0000"), 
                            "updated_at" : ISODate("2020-07-14T21:25:05.126+0000")
                        }, 
                        {
                            "_id" : ObjectId("5f0dc0442022e93af3383170"), 
                            "name" : "Light", 
                            "value" : null, 
                            "created_at" : ISODate("2020-07-14T21:25:08.451+0000"), 
                            "updated_at" : ISODate("2020-07-14T21:25:08.451+0000")
                        }
                    ], 
                    "switches" : [

                    ]
                }
            ]
        }
    ]
}

我要使用以下查询更新value数组内的sensors字段:

db.users.updateOne(
    {
        '_id': ObjectId("5f0c64e4dd0a36b93c7deafa"), 
        "rooms.devices.sensors._id": ObjectId("5f0db478ca203bde99d2438e"), 
    }, 
    {$set: {'rooms.$[].devices.$[].sensors.$.value': 21.0}}
)

但是结果仅更新了数组的最后一个索引,其名称为“ Light”。考虑到给定的_id用于温度文档,它应该是“温度”。如何仅更新一个文档?

0 个答案:

没有答案