2017-08-12 53 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