2017-08-12 52 views
0

我有一個酒店集合,其一個文檔看起來是這樣的 -更新mongoDB中列表的兩級子文檔?

{ 
    "_id" : "HOTEL_1", 
    "name" : "Decent hotel", 
    "chainId" : "CHN123", 
    "rooms" : [ 
     { 
      "id" : "ROM1", 
      "name" : "decent rooms", 
      "ratePlans" : [ 
       { 
        "ratePlanId" : "RPNB1191989873C2G", 
        "status" : "INACTIVE", 
        "marginPart" : { 
         "marginType" : "PERCENTAGE", 
         "margin" : "32" 
        } 
       }, 
       { 
        "ratePlanId" : "RPNE0992HBG6I0GE8", 
        "status" : "INACTIVE", 
        "marginPart" : { 
         "marginType" : "PERCENTAGE", 
         "margin" : "32" 
        } 
       } 
      ] 
     }, 
     { 
      "id" : "ROM2", 
      "name" : "another decent rooms" 
      "ratePlans" : [] 
     } 
    ] 
} 

我需要更新狀態爲ACTIVE的所有特定條件像chainId房間的所有資費套餐。

我試着用這個,但失敗 -

db.hotel.updateMany({ "chainId" : "CHN_123"},{$set : {"rooms.$ratePlans.$status" : "ACTIVE" }}); 

我也想作爲共同的價值說50%,所有這些利率更新保證金。

回答

0

相反updateMany的嘗試下面的查詢

db.hotel.update({ "chainId" : "CHN_123"},{$set : {"rooms.$ratePlans.$status" : "ACTIVE" }},{multi:true,upsert:false},function(err,doc){ 
     console.log(doc) 
}); 

它的工作原理永遠!

+0

得到此錯誤:指定upsert和multi與對象時,第四個參數必須爲空。 – Bharat

+0

您正在使用我猜的終端,所以請嘗試 db.hotel.update({「chainId」:「CHN_123」},{$ set:{「rooms。$ ratePlans。$ status」:「ACTIVE」}},{multi :true,upsert:false}) –

+0

仍然沒有運氣 - ''不能使用部分(房間的房間。$ ratePlans。$ status)來遍歷元素'這是我以前遇到的同樣的錯誤。 – Bharat