Couchbase N1QL - 使用键嵌套数组
Couchbase N1QL - Nest array using keys
我是 Couchbase 和 N1QL 语法的新手,我遇到了一个问题。
假设我们有 3 种类型的文档:
类型 A 的文档 1,键 = typeA:Doc1
{
"type": "typeA"
"id": "Doc1",
"sequences": [
"typeB:Doc2"
]
}
类型 B 的文档 2,键 = typeB:Doc2
{
"id": "Doc2",
"processors": [
{
"order": 1,
"id": "typeC:Doc3"
}
]
}
带键的 TypeC 的 Doc3 = typeC:Doc3
{
"id": "Doc3",
"prop": "value"
}
我想要实现的是通过文档键嵌套这 3 个对象,以便拥有具有这种结构的唯一文档:
{
"id": "Doc1",
"sequences": [
{
"id": "Doc2",
"processors": [
{
"order": 1,
"id": "Doc3",
"prop": "value"
}
]
}
]
我所做的是嵌套前两个文档以获得部分结果。但我也在尝试整合第三个文件。
这是我的尝试:
SELECT dev.*,
ARRAY sq_i FOR sq_i IN prseq END AS sequences
FROM data dev
NEST data prseq ON KEYS dev.sequences
WHERE dev.type = 'TypeA'
谁能帮我做第三层嵌套?
谢谢。
使用子查询
SELECT dt.*,
(SELECT ds.*,
(ARRAY OBJECT_ADD((SELECT RAW dp FROM data AS dp USE KEYS v.id)[0], "order", v.`order`)
FOR v IN ds.processors
END) AS processors
FROM data AS ds USE KEYS dt.sequences) AS sequences
FROM data AS dt
WHERE dt.type = 'TypeA';
我是 Couchbase 和 N1QL 语法的新手,我遇到了一个问题。 假设我们有 3 种类型的文档:
类型 A 的文档 1,键 = typeA:Doc1
{
"type": "typeA"
"id": "Doc1",
"sequences": [
"typeB:Doc2"
]
}
类型 B 的文档 2,键 = typeB:Doc2
{
"id": "Doc2",
"processors": [
{
"order": 1,
"id": "typeC:Doc3"
}
]
}
带键的 TypeC 的 Doc3 = typeC:Doc3
{
"id": "Doc3",
"prop": "value"
}
我想要实现的是通过文档键嵌套这 3 个对象,以便拥有具有这种结构的唯一文档:
{
"id": "Doc1",
"sequences": [
{
"id": "Doc2",
"processors": [
{
"order": 1,
"id": "Doc3",
"prop": "value"
}
]
}
]
我所做的是嵌套前两个文档以获得部分结果。但我也在尝试整合第三个文件。
这是我的尝试:
SELECT dev.*,
ARRAY sq_i FOR sq_i IN prseq END AS sequences
FROM data dev
NEST data prseq ON KEYS dev.sequences
WHERE dev.type = 'TypeA'
谁能帮我做第三层嵌套? 谢谢。
使用子查询
SELECT dt.*,
(SELECT ds.*,
(ARRAY OBJECT_ADD((SELECT RAW dp FROM data AS dp USE KEYS v.id)[0], "order", v.`order`)
FOR v IN ds.processors
END) AS processors
FROM data AS ds USE KEYS dt.sequences) AS sequences
FROM data AS dt
WHERE dt.type = 'TypeA';