我有以下文档与doc key cart :: 16
{
"_type": "NSCart",
"_metadata": {
"created_at": 1473075845614,
"updated_at": null
},
"_id": "f28bc609-6aba-47e5-8e83-5bee2397566f",
"userId": "B1HvmsLj57c9235c",
"items" : [{
"itemId": "N1d55j8m4xr",
"seller": "ByD9T4Ks57cbbd8e",
"license": null,
"basePrice": 0
},{
"itemId": "L9nd6Dswl4v",
"seller": "ByD9T4Ks57cbbd8e",
"license": E122,
"basePrice": 500,
"eCharge": 10,
"total": 510
}]
}
现在我要更新项目ID为N1d55j8m4xr的doc
{
"itemId": "N1d55j8m4xr",
"seller": "ByD9T4Ks57cbbd8e",
"license": null,
"basePrice": 590,
"eCharge": 18,
"total": 608
}
在n1ql中有方法array_replace但没有获得所需的输出。
答案 0 :(得分:3)
使用N1QL文档中的以下语法。
UPDATE mybucket SET x.field = newvalue FOR x IN myarray WHEN x.id = myid END;
答案 1 :(得分:0)
update `buc_name` set items =
CASE
WHEN "N1d55j8m4xr" IN items [*].itemId
THEN (ARRAY CASE WHEN itm.itemId = "N1d55j8m4xr"
THEN {
"itemId": "N1d55j8m4xr",
"seller": "ByD9T4Ks57cbbd8e",
"license": null,
"basePrice": 590,
"eCharge": 18,
"total": 608
}
ELSE itm
END
FOR itm IN items END) end where "N1d55j8m4xr" IN items [*].itemId