Couchbase N1QL 查询对象数组中的 select 值
Couchbase N1QL query to select value from object array
从对象数组中 select 取值的正确方法是什么?
想要检索不同的评论列表。
[
"comment 1",
"comment 2",
"comment 3",
"comment 4"
]
当前 N1QL 查询
SELECT links[*].comment FROM `my-db`
WHERE type = "links" AND ANY l IN links SATISFIES l.comment IS NOT MISSING END
结果
[
{
"comment": [
"comment 1"
]
},
{
"comment": [
"comment 1",
"comment 2"
]
},
{
"comment": [
"comment 3",
"comment 4"
]
}
]
给定一系列包含评论字段的文档:
{
"type": "links",
"links": [
{
"comment": "comment 1"
}
]
}
{
"type": "links",
"links": [
{
"comment": "comment 1"
},
{
"comment": "comment 2"
}
]
}
{
"type": "links",
"links": [
{
"comment": "comment 3"
},
{
"comment": "comment 4"
}
]
}
使用 UNNEST
SELECT DISTINCT RAW l.comment
FROM `my-db` AS m
UNNEST m.links AS l
WHERE m.type = "links" AND l.comment IS NOT NULL;
或
If data set is not too large.
SELECT RAW ARRAY_DISTINCT(ARRAY_FLATTEN(ARRAY_AGG(m.links[*].comment) ,2))
FROM `my-db` AS m
WHERE m.type = "links";
从对象数组中 select 取值的正确方法是什么?
想要检索不同的评论列表。
[
"comment 1",
"comment 2",
"comment 3",
"comment 4"
]
当前 N1QL 查询
SELECT links[*].comment FROM `my-db`
WHERE type = "links" AND ANY l IN links SATISFIES l.comment IS NOT MISSING END
结果
[
{
"comment": [
"comment 1"
]
},
{
"comment": [
"comment 1",
"comment 2"
]
},
{
"comment": [
"comment 3",
"comment 4"
]
}
]
给定一系列包含评论字段的文档:
{
"type": "links",
"links": [
{
"comment": "comment 1"
}
]
}
{
"type": "links",
"links": [
{
"comment": "comment 1"
},
{
"comment": "comment 2"
}
]
}
{
"type": "links",
"links": [
{
"comment": "comment 3"
},
{
"comment": "comment 4"
}
]
}
使用 UNNEST
SELECT DISTINCT RAW l.comment
FROM `my-db` AS m
UNNEST m.links AS l
WHERE m.type = "links" AND l.comment IS NOT NULL;
或
If data set is not too large.
SELECT RAW ARRAY_DISTINCT(ARRAY_FLATTEN(ARRAY_AGG(m.links[*].comment) ,2))
FROM `my-db` AS m
WHERE m.type = "links";