如何解决Couchbase N1QL 查询以从对象数组中选择值
从对象数组中选择值的正确方法是什么?
想要检索不同的评论列表。
[
"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";
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。