2017-09-12 148 views
0

下面的查詢由MongoDB的蒙戈DB更新操作

錯誤地更新

查詢:

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") 
} 
+0

嘗試沒有'「Objects.out.interface」:「down''。結果是什麼? – Rumoku

+0

同樣的結果,因爲兩個對象都有'down' – user3041095

+0

@ user3041095,這是不正確的。如果你刪除''Objects.out.interface':'down'',它會更新id爲4的那個。 –

回答

0

因爲你有子文檔2個條件:

'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 
     } 
    } 
} 
+0

謝謝lok,工作的爐排。 – user3041095