我想检索和处理一些Couchbase文档,即检索和进行一些字符串处理,但我在网上看不到初学者的示例。
我在这里使用正确的方法还是有更简单的方法...?
SELECT meta().id
FROM bucket_foo
where meta().id like "%-foobar"
ORDER BY meta().id
这给了我查询结果...
[
{
"id": "172.26.138.52-foobar"
},
{
"id": "172.26.138.53-foobar"
},
{
"id": "172.26.138.64-foobar"
}
]
我想删除后缀,即将其转换为类似的内容:
[
{
"id": "172.26.138.52"
},
{
"id": "172.26.138.53"
},
{
"id": "172.26.138.64"
}
]
因此,在阅读https://developer.couchbase.com/documentation/server/4.0/n1ql/n1ql-language-reference/stringfun.html之后,我尝试了...
SELECT REPLACE(
(SELECT meta().id FROM foo where meta().id like "%-foobar" ORDER BY meta().id)
, "-foobar", "") as replace_all;
但我知道
[
{
"replace_all": null
}
]
[我是Couchbase的新手。]
答案 0 :(得分:3)
您使用的是正确的方法,子查询返回数组,并且无法在数组上进行替换。在这种情况下,不需要子查询。
您可以使用RTRIM()或REPLACE()或SUBSTR()或类似函数
SELECT RTRIM(meta().id,"-foobar") AS id
FROM bucket_foo
where meta().id like "%-foobar"
ORDER BY id;
OR
SELECT REPLACE(meta().id,"-foobar","") AS id
FROM bucket_foo
where meta().id like "%-foobar"
ORDER BY id;
OR
SELECT SUBSTR(meta().id,0,LENGTH(meta().id)-LENGTH("-foobar")) AS id
FROM bucket_foo
where meta().id like "%-foobar"
ORDER BY id;