Mongo DB更新操作

时间:2017-09-12 14:50:24

标签: mongodb mongodb-query

以下查询错误地通过mongoDB更新

查询:

d.update({'Objects.out.interface': 'down', 'IP': '192.168.106.11', 'INID': 19, 'SESSION': 1, 'Objects.id': 4}, {$set: {'Objects.$.score':888888}})

为什么我的分数888888在Objects.id:2中更新不在Objects.id:4?

更新结果:

{
        "_id" : ObjectId("59b7ebec9315080ac2801468"),
        "SESSION" : 1,
        "INID" : 19,
        "IP" : "192.168.106.11",
        "Hostname" : "Npppp",
        "JOBNAME" : "Nexus2-12-September-19-45-08",
        "Authentication" : "{\"username\":\"gowtham\",\"password\":\"pppppp\"}",
        "Objects" : [
                {
                        "name" : "self",
                        "out" : {
                                "status" : "reachable"
                        },
                        "type" : "self_check",
                        "id" : 1,
                        "rank" : [
                                {
                                        "regex" : {
                                                "status" : "down"
                                        },
                                        "score" : 0
                                },
                                {
                                        "regex" : {
                                                "status" : "reachable"
                                        },
                                        "score" : 100
                                }
                        ],
                        "monitor" : "self",
                        "score" : 100
                },
                {
                        "name" : "Eth1/1",
                        "out" : {
                                "interface" : "down"
                        },
                        "type" : "cis_sw_int",
                        "id" : 2,
                        "rank" : [
                                {
                                        "regex" : {
                                                "interface" : "down"
                                        },
                                        "score" : 0
                                },
                                {
                                        "regex" : {
                                                "interface" : "up"
                                        },
                                        "score" : 100
                                }
                        ],
                        "monitor" : "bits,duplex,speed,error",
                        "score" : 888888
                },
                {
                        "name" : "Eth1/37",
                        "out" : {
                                "interface" : "down"
                        },
                        "type" : "cis_sw_int",
                        "id" : 3,
                        "rank" : [
                                {
                                        "regex" : {
                                                "interface" : "down"
                                        },
                                        "score" : 0
                                },
                                {
                                        "regex" : {
                                                "interface" : "up"
                                        },
                                        "score" : 100
                                }
                        ],
                        "monitor" : "bits,duplex,speed,error"
                },
                {
                        "name" : "Eth1/46",
                        "out" : {
                                "interface" : "down"
                        },
                        "type" : "cis_sw_int",
                        "id" : 4,
                        "rank" : [
                                {
                                        "regex" : {
                                                "interface" : "down"
                                        },
                                        "score" : 0
                                },
                                {
                                        "regex" : {
                                                "interface" : "up"
                                        },
                                        "score" : 100
                                }
                        ],
                        "monitor" : "bits,duplex,speed,error"
                }
        ],
        "timeout" : 10,
        "TD" : ISODate("2017-09-12T19:45:08.743Z")
}

1 个答案:

答案 0 :(得分:0)

因为您有两个子文档条件:

'Objects.out.interface': 'down', 
'Objects.id': 4

Positional operator使用第一个匹配的子文档

  

位置$运算符充当与查询文档匹配的第一个元素的占位符

在您的情况下,'Objects.out.interface': 'down'的第一个匹配是ID为2的那个。

您需要更改过滤器以使用$elemMatch来使用这两个条件来标识子文档:

{
    'IP': '192.168.106.11', 
    'INID': 19, 
    'SESSION': 1, 
    'Objects': {
        $elemMatch: {
            'out.interface': 'down', 
            'id': 4
        }
    }
}