mongodb - Mongo DB update operation -
below query wrongly update mongodb
query:
d.update({'objects.out.interface': 'down', 'ip': '192.168.106.11', 'inid': 19, 'session': 1, 'objects.id': 4}, {$set: {'objects.$.score':888888}}) why score 888888 updated in objects.id:2 not in objects.id:4 ?
update result:
{ "_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") }
because have 2 conditions subdocuments:
'objects.out.interface': 'down', 'objects.id': 4 positional operator uses first matching sub-document
the positional $ operator acts placeholder first element matches query document,
in case first match 'objects.out.interface': 'down' 1 id:2.
you need change filter use $elemmatch use both conditions identify subdocument:
{ 'ip': '192.168.106.11', 'inid': 19, 'session': 1, 'objects': { $elemmatch: { 'out.interface': 'down', 'id': 4 } } }
Comments
Post a Comment