Couchbase Coucbbase中的复杂N1QL查询
我在couchbase中的同一个桶中有以下两种类型的文档,称为“实体”:Couchbase Coucbbase中的复杂N1QL查询,couchbase,n1ql,Couchbase,N1ql,我在couchbase中的同一个桶中有以下两种类型的文档,称为“实体”: { "documentType": "person", "id": "4f3567cd-782d-4456-aabd-ff5faf071476", "personal": { "gender": "Male", "dob": "1984-05-22", "firstName": "Peter", "lastName": "Smith", "idNumber": "4915
{
"documentType": "person",
"id": "4f3567cd-782d-4456-aabd-ff5faf071476",
"personal": {
"gender": "Male",
"dob": "1984-05-22",
"firstName": "Peter",
"lastName": "Smith",
"idNumber": "4915040000111"
}
}
以及:
人员文档描述人员,链接表定义人员之间的链接。一个家长可以有多个孩子,而一个家长将为他拥有的每个孩子都有一个链接文档
问题:给定家长id,返回该家长的每个孩子的所有个人信息,例如
[
{
"id": "000151ef-f4b9-4cd4-bf3b-d5b07d79ed21",
"personal": {
"gender": "Male",
"dob": "2004-05-22",
"firstName": "Chris",
"lastName": "Smith",
"idNumber": "0415050000111"
}
},
{
...
}
]
到目前为止:我能够使用以下工具获得所有儿童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似乎无法对子查询执行联接。有没有一种方法可以使用N1QL实现这一点?目前,我唯一的解决方案是获取ID列表,然后运行另一个查询以获取子项。这非常有效-正是所需的。我知道我们不应该这样说“谢谢”,但亲爱的同事,如果你在附近,我会给你买一杯啤酒(两杯),如果你不喝熊,那么你可以选择任何饮料。非常感谢。
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
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