Coucbbase 中的复杂 N1QL 查询
Complex N1QL query in Coucbbase
我在名为 'entities' 的同一存储桶中的 couchbase 中有以下两种类型的文档:
{
"documentType": "person",
"id": "4f3567cd-782d-4456-aabd-ff5faf071476",
"personal": {
"gender": "Male",
"dob": "1984-05-22",
"firstName": "Peter",
"lastName": "Smith",
"idNumber": "4915040000111"
}
}
和:
{
"id": "09690237-9ef5-4381-93dc-7312f7417f82",
"documentType": "link",
"entities": [
{
"id": "000151ef-f4b9-4cd4-bf3b-d5b07d79ed20",
"type": "parent",
},
{
"id": "000151ef-f4b9-4cd4-bf3b-d5b07d79ed21",
"type": "child"
}
]
}
人物文档描述了一个人,link table 定义了人与人之间的 link。一个 parent 可以有多个 children,而一个 parent 将有一个 link 文档,他有每个 child。
问题:给定一个parent id,return那个parent的每个child人的所有个人信息,例如
[
{
"id": "000151ef-f4b9-4cd4-bf3b-d5b07d79ed21",
"personal": {
"gender": "Male",
"dob": "2004-05-22",
"firstName": "Chris",
"lastName": "Smith",
"idNumber": "0415050000111"
}
},
{
...
}
]
到目前为止:我能够使用以下方法获得所有 children ID 的列表:
select array_flatten(array_agg(array entity.id for entity in entities.entities when entity.type = 'parent' end), 3) as entity
from entities
where entities.docuementType = 'link'
and any entity within entities satisfies entity.id = '01f6a9eb-0d7e-4495-a90f-f96a38aef621'
and entity.type='parent' end
但是,couchbase 似乎无法在 sub-queries 上执行连接。有没有一种方法可以使用 N1QL 来完成?目前我唯一的解决方案是获取 ID 列表,然后 运行 另一个查询来获取 children.
SELECT p
FROM entities AS l
JOIN entities AS p
ON KEYS ARRAY e.id FOR e IN l.entities WHEN e.type = "child" END
WHERE l.docuementType = 'link'
AND ANY entity IN l.entities
SATISFIES entity.id = '01f6a9eb-0d7e-4495-a90f-f96a38aef621'
AND entity.type='parent' END
我在名为 'entities' 的同一存储桶中的 couchbase 中有以下两种类型的文档:
{
"documentType": "person",
"id": "4f3567cd-782d-4456-aabd-ff5faf071476",
"personal": {
"gender": "Male",
"dob": "1984-05-22",
"firstName": "Peter",
"lastName": "Smith",
"idNumber": "4915040000111"
}
}
和:
{
"id": "09690237-9ef5-4381-93dc-7312f7417f82",
"documentType": "link",
"entities": [
{
"id": "000151ef-f4b9-4cd4-bf3b-d5b07d79ed20",
"type": "parent",
},
{
"id": "000151ef-f4b9-4cd4-bf3b-d5b07d79ed21",
"type": "child"
}
]
}
人物文档描述了一个人,link table 定义了人与人之间的 link。一个 parent 可以有多个 children,而一个 parent 将有一个 link 文档,他有每个 child。
问题:给定一个parent id,return那个parent的每个child人的所有个人信息,例如
[
{
"id": "000151ef-f4b9-4cd4-bf3b-d5b07d79ed21",
"personal": {
"gender": "Male",
"dob": "2004-05-22",
"firstName": "Chris",
"lastName": "Smith",
"idNumber": "0415050000111"
}
},
{
...
}
]
到目前为止:我能够使用以下方法获得所有 children ID 的列表:
select array_flatten(array_agg(array entity.id for entity in entities.entities when entity.type = 'parent' end), 3) as entity
from entities
where entities.docuementType = 'link'
and any entity within entities satisfies entity.id = '01f6a9eb-0d7e-4495-a90f-f96a38aef621'
and entity.type='parent' end
但是,couchbase 似乎无法在 sub-queries 上执行连接。有没有一种方法可以使用 N1QL 来完成?目前我唯一的解决方案是获取 ID 列表,然后 运行 另一个查询来获取 children.
SELECT p
FROM entities AS l
JOIN entities AS p
ON KEYS ARRAY e.id FOR e IN l.entities WHEN e.type = "child" END
WHERE l.docuementType = 'link'
AND ANY entity IN l.entities
SATISFIES entity.id = '01f6a9eb-0d7e-4495-a90f-f96a38aef621'
AND entity.type='parent' END