如何使用 n1ql 将变量传递给子查询?
How to pass a variable into Subquery with n1ql?
在我的情况下,entreprise(company) 可以有多个站点(filiales),我想获取格式数组的所有filiales。
在json entreprise(company)中,没有sites(filiales)的信息,在json sites(filiales)中,有entreprise(company)uid。
Json 企业(公司):
{
"type": "entreprise",
"dateUpdate": 1481716305279,
"owner": {
"type": "user",
"uid": "PNnqarPqSdaxmEJ4DoMv-A"
}
}
Json 站点(子目录):
{
"type": "site",
"entreprise": {
"uid": "3c0CstzsTjqPdycL5yYzJQ",
"type": "entreprise"
},
"nom": "test"
}
我试过的查询:
SELECT
META(entreprise).id as uid,
ARRAY s FOR s IN (SELECT d.* FROM default d WHERE d.type = "site" AND d.entreprise.uid = uid) END as sites,
entreprise.*
FROM default entreprise
WHERE entreprise.type = "entreprise";
结果:错误
{
"code": 5010,
"msg": "Error evaluating projection. - cause: FROM in correlated subquery must have USE KEYS clause: FROM default."
}
然后我使用别名:
SELECT
META(entreprise).id as uid,
ARRAY s FOR s IN (SELECT d.* FROM default d WHERE d.type = "site" AND d.entreprise.uid = META(entreprise).id) END as sites,
entreprise.*
FROM default entreprise
WHERE entreprise.type = "entreprise";
结果:站点数组为空。
您需要执行从站点到企业的索引连接。参见 https://dzone.com/articles/join-faster-with-couchbase-index-joins
之后,使用 GROUP BY 和 ARRAY_AGG() 将站点收集到数组中。
首先,您必须为站点文档创建索引:
CREATE INDEX site_ent_idx ON default(entreprise.uid) WHERE type="site";
然后更改您的查询以使用新索引:
SELECT
META(entreprise).id as uid,
ARRAY s FOR s IN (
SELECT site.*
FROM default as ent USE KEYS META(entreprise).id
JOIN default as site ON KEY site.entreprise.uid FOR ent
) END as sites,
entreprise.*
FROM default entreprise
WHERE entreprise.type = "entreprise"
这个解决方案应该可以满足您的需求。
在我的情况下,entreprise(company) 可以有多个站点(filiales),我想获取格式数组的所有filiales。
在json entreprise(company)中,没有sites(filiales)的信息,在json sites(filiales)中,有entreprise(company)uid。 Json 企业(公司):
{
"type": "entreprise",
"dateUpdate": 1481716305279,
"owner": {
"type": "user",
"uid": "PNnqarPqSdaxmEJ4DoMv-A"
}
}
Json 站点(子目录):
{
"type": "site",
"entreprise": {
"uid": "3c0CstzsTjqPdycL5yYzJQ",
"type": "entreprise"
},
"nom": "test"
}
我试过的查询:
SELECT
META(entreprise).id as uid,
ARRAY s FOR s IN (SELECT d.* FROM default d WHERE d.type = "site" AND d.entreprise.uid = uid) END as sites,
entreprise.*
FROM default entreprise
WHERE entreprise.type = "entreprise";
结果:错误
{
"code": 5010,
"msg": "Error evaluating projection. - cause: FROM in correlated subquery must have USE KEYS clause: FROM default."
}
然后我使用别名:
SELECT
META(entreprise).id as uid,
ARRAY s FOR s IN (SELECT d.* FROM default d WHERE d.type = "site" AND d.entreprise.uid = META(entreprise).id) END as sites,
entreprise.*
FROM default entreprise
WHERE entreprise.type = "entreprise";
结果:站点数组为空。
您需要执行从站点到企业的索引连接。参见 https://dzone.com/articles/join-faster-with-couchbase-index-joins
之后,使用 GROUP BY 和 ARRAY_AGG() 将站点收集到数组中。
首先,您必须为站点文档创建索引:
CREATE INDEX site_ent_idx ON default(entreprise.uid) WHERE type="site";
然后更改您的查询以使用新索引:
SELECT
META(entreprise).id as uid,
ARRAY s FOR s IN (
SELECT site.*
FROM default as ent USE KEYS META(entreprise).id
JOIN default as site ON KEY site.entreprise.uid FOR ent
) END as sites,
entreprise.*
FROM default entreprise
WHERE entreprise.type = "entreprise"
这个解决方案应该可以满足您的需求。