如何使用Couchbase查询创建Couchbase文档的键值对?

huangapple go评论56阅读模式
英文:

How to create a key value pairs of couchbase document using couchbase query

问题

以下是您的翻译:

问题描述:
我正在为我的项目编写Couchbase查询,遇到了一个查询的问题。以下是问题的详细信息。

我有一个名为“workflow”的桶,里面有一些文档:

{
type = "task",
status = "Completed",
appId = "72689c67-f5d1-474a-8802-77f332144b12",
taskNo = 1
},
{
type = "task",
status = "Completed",
appId = "72689c67-f5d1-474a-8802-77f332144b12",
taskNo = 2
},
{
type = "task",
status = "New",
appId = "72689c67-f5d1-474a-8802-77f332144b12",
taskNo = 3
},
{
type = "task",
status = "New",
appId = "72689c67-f5d1-474a-8802-77f332144b12",
taskNo = 4
},
{
type = "task",
status = "New",
appId = "72689c67-f5d1-474a-8802-77f332144b12",
taskNo = 5
}

我编写了以下查询:

select status, count(*) from workflow where type="task" and appId="72689c67-f5d1-474a-8802-77f332144b12" and status!="" group by status;

结果:

[
{
"$1": 2,
"status": "Completed"
},
{
"$1": 3,
"status": "New"
}
]

期望结果:

[
{
"Completed": 2
},
{
"New": 3
}
]

是否有一种方法可以获得期望的结果?任何线索都将非常有帮助。这将使我免于运行不必要的for循环,从而在扩展时优化我的代码。还有没有其他方法替代以下语句?

status!=""

英文:

I am writing couchbase query for my project and facing problem in 1 query. Below is the details of the problem.

I have a bucket name as "workflow" and I have few documents in it :-

{
     type = "task",
     status = "Completed",
     appId = "72689c67-f5d1-474a-8802-77f332144b12",
     taskNo = 1
},
{
     type = "task",
     status = "Completed",
     appId = "72689c67-f5d1-474a-8802-77f332144b12",
     taskNo = 2
},
{
     type = "task",
     status = "New",
     appId = "72689c67-f5d1-474a-8802-77f332144b12",
     taskNo = 3
},
{
     type = "task",
     status = "New",
     appId = "72689c67-f5d1-474a-8802-77f332144b12",
     taskNo = 4
},
{
     type = "task",
     status = "New",
     appId = "72689c67-f5d1-474a-8802-77f332144b12",
     taskNo = 5
}

I have written a query :-

select status, count(*) from workflow where type="task" and appId="72689c67-f5d1-474a-8802-77f332144b12" and status!="" group by status;

Result :-

[
   {
       "$1": 2,
       "status": "Completed"
   },
   {
       "$1": 3,
       "status": "New"
   }
]

Expected Result :-

[
    {
        "Completed": 2
    },
    {
        "New": 3
    }
]

Is there a way that we can get the expected result? Any leads will be really very helpful. I will be saved from running unnecessary for loop which will optimise my code during scaling. And any other alternative for the below statement?

status!=""

答案1

得分: 2

select RAW {status:count(1)} 
from workflow 
where type="task" 
      and appId="72689c67-f5d1-474a-8802-77f332144b12" 
      and status != "" 
group by status;

OR

SELECT RAW OBJECT v.status:v.cnt 
           FOR v IN (select status, count(1) AS cnt 
                     from workflow 
                     where type="task" 
                           and appId="72689c67-f5d1-474a-8802-77f332144b12" 
                           and status != "" 
                     group by status)
            END;

You can use status > "" AND status < []
英文:
select RAW {status:count(1)} 
from workflow 
where type=&quot;task&quot; 
      and appId=&quot;72689c67-f5d1-474a-8802-77f332144b12&quot; 
      and status != &quot;&quot; 
group by status;

OR

SELECT RAW OBJECT v.status:v.cnt 
           FOR v IN (select status, count(1) AS cnt 
                     from workflow 
                     where type=&quot;task&quot; 
                           and appId=&quot;72689c67-f5d1-474a-8802-77f332144b12&quot; 
                           and status != &quot;&quot; 
                     group by status)
            END;

You can use status > "" AND status < []

huangapple
  • 本文由 发表于 2023年4月4日 16:51:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75927344.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定