如何在CouchDB中编写视图以表示“不在”视图。或“通过count(*)= 1分组”? [英] How to write a view in CouchDB to represent "not in" or "group by having count(*) = 1"?

查看:72
本文介绍了如何在CouchDB中编写视图以表示“不在”视图。或“通过count(*)= 1分组”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以关系数据库为例,给定以下两个表,其中 tableA tableB 中的行具有相同的值表示相同的事物,但处于不同的状态。因此,对于ID = 1,这件事已经通过了stage1和2。但是对于ID = 2,这件事只经过了stage1。

Using relational db as an example, given two tables like below, where when rows in tableA and tableB have the same values, they represent the same "thing" but in different "state". So for ID=1, this thing has gone through stage1 and 2. But for ID=2, this thing has only gone through stage1.

tableA (Id, columnA, columnB)
         1, "a", "b"
         2, "x", "y"
         3, "z", "a"
tableB (Id, columnA, columnB)
         1, "e", "f"

我想在 tableB 不存在的所有 tableA 行$ c>。

I want to find all the rows from tableA that don't exist in tableB.

select  a.*
from    tableA a
  left join
        tableB b
  on    a.Id = b.Id
where   b.Id is null

因此,上面的SQL将显示tableA的第2行和第3行。

So above SQL will show rows 2 and 3 from tableA.

如何在CouchDB中执行类似的操作?假设我有4个如下所示的文档。

How can I do similar things in CouchDB? Say I have 4 docs that look like below.

{ "_id":"a-1", "type":"A", "correlation_id": "1" }
{ "_id":"a-2", "type":"A", "correlation_id": "2" }
{ "_id":"a-3", "type":"A", "correlation_id": "3" }
{ "_id":"b-1", "type":"B", "correlation_id": "1" }

如何创建仅显示文档 id的视图= a-2和a-3 ?我不想过滤,只想显示所有没有 B型的文档。我可以做一个 group by和count(*)与视图等效,但是我不能做一个 group by,具有count(*) = 1 等效项。

How can I create a view that only show docs id = a-2 and a-3? I don't want to filter, just want to show all docs that haven't got type B. I could kinda do a group by and count(*) equivalent with view, but I can't do a group by, having count(*) = 1 equivalent.

我正在使用CouchDB 3.0。

I'm using CouchDB 3.0.

推荐答案

您可以编写一个视图:

function (doc) {
  emit(doc.type, 1);
}

,然后使用键 A查询视图 include_docs = true ,如果您想要这些文档的全部内容。

and then query the view using the key "A" and include_docs=true if you wanted the whole content of those documents.

如果您不仅要 A ,而且要除了B之外的一切 ,您可以从开始查询到B,然后从B结束并以这种方式获取所有文档。

If you not only want "A" but "everything but B" you can query from start to B and then from B to end and get all the documents that way.

根据您的设置,使用 group_level = 1 查询视图可能会更容易code>,以便您获取所有键,然后遍历它们(不包括B)以获取您感兴趣的其余信息。

Depending on your setup it might be easier to query the view with group_level=1 so you get all the keys and then loop through them excluding B to get the rest of the info you're interested in.

这篇关于如何在CouchDB中编写视图以表示“不在”视图。或“通过count(*)= 1分组”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆