我正在尝试编写N1QL更新查询。我想从“ vs”值为“ k_a”的数组中删除这些对象。 这是我的样本文件。
{
"ADD": {
"k_2": [
{
"allowed": false,
"vs": "k_a"
},
{
"allowed": false,
"vs": "k_d"
}
],
"k_9": [
{
"allowed": false,
"vs": "k_f"
},
{
"allowed": true,
"vs": "k_a"
}
]
},
"REMOVE": {
"k_4": [
{
"allowed": true,
"vs": "k_a"
}
],
"k_7": [
{
"allowed": false,
"vs": "k_g"
},
{
"allowed": true,
"vs": "k_a"
}
]
}
}
我已经尝试过此查询-
UPDATE bucket_name sp
SET obj = ARRAY v FOR v IN obj WHEN v.vs != "k_a" END
FOR obj IN OBJECT_VALUES(sp.ADD) END WHERE META(sp).id = "SPT|O|789"
但可能我做错了事。
这是我期望更新后的文档-
{
"ADD": {
"k_2": [
{
"allowed": false,
"vs": "k_d"
}
],
"k_9": [
{
"allowed": false,
"vs": "k_f"
}
]
},
"REMOVE": {
"k_4": [],
"k_7": [
{
"allowed": false,
"vs": "k_g"
}
]
}
}
注意-此处的关键字“ ADD”和“ REMOVE”是静态的,但形式为“ k_number”或“ k_letter”的任何形式都是动态的,并且可以是任何形式。
我们将不胜感激任何帮助。
答案 0 :(得分:1)
Where子句控制文档是否必须突变。 SET子句控制要更改的内容。您需要where子句,以避免不必要的突变(例如:如果第二次重复相同的更新,则不应进行任何更新,即,除非有人更改了文档,否则变异计数应为0。)
UPDATE sourceprioritization sp USE KEYS "SPT|O|789"
SET sp.[obj1.name].[obj.name] = ARRAY v FOR v IN obj.val WHEN v.vs != "k_a" END
FOR obj IN OBJECT_PAIRS(obj1.val)
FOR obj1 IN OBJECT_PAIRS(sp) WHEN obj1.name IN ["ADD", "REMOVE"] AND "k_a" IN obj.val[*].vs END
WHERE ANY obj1 IN OBJECT_PAIRS(sp) SATISFIES obj1.name IN ["ADD", "REMOVE"]
AND (ANY obj IN OBJECT_PAIRS(obj1.val) SATISFIES "k_a" IN obj.val[*].vs END) END;
OR
UPDATE sourceprioritization sp USE KEYS "SPT|O|789"
SET obj1.val.[obj.name] = ARRAY v FOR v IN obj.val WHEN v.vs != "k_a" END
FOR obj IN OBJECT_PAIRS(obj1.val)
FOR obj1 IN OBJECT_PAIRS(sp) WHEN obj1.name IN ["ADD", "REMOVE"] AND "k_a" IN obj.val[*].vs END
WHERE ANY obj1 IN OBJECT_PAIRS(sp) SATISFIES obj1.name IN ["ADD", "REMOVE"]
AND (ANY obj IN OBJECT_PAIRS(obj1.val) SATISFIES "k_a" IN obj.val[*].vs END) END;