N1QL 使用子查询更新文档

N1QL update document with subquery

我正在尝试对 "Country" 文档进行反规范化,其中 "regionName" 字段对应于另一个 "Region" 文档的 "name"。我的 N1QL 查询如下,但它不起作用

update test as t1 set regionName = (
 select raw name from test as t2 where `_class`="Region"
) where `_class`="Country" and t1.regionCode=t2.code RETURNING *;

有什么帮助吗?

N1QL 没有 UPDATE JOINS。您不能在 UPDATE WHERE 子句中使用 SET CLAUSE 子查询源。

使用合并

MERGE test AS m 
USING test AS s
ON s.`_class`="Region" AND m.`_class`="Country" AND m.regionCode=s.code
WHEN MATCHED THEN m.regionName = s.name;

https://blog.couchbase.com/ansi-join-enhancements-and-ansi-merge/

CB 6.0

MERGE test AS m 
USING  (SELECT META(c).id, r.name
        FROM test AS r
        JOIN test AS c 
        ON r.`_class`="Region" AND c.`_class`="Country" 
           AND r.regionCode = c.code) AS s
ON KEY s.id
WHEN MATCHED THEN m.regionName = s.name;