CouchDB相当于Sql NOT IN吗? [英] CouchDB equivalent of Sql NOT IN?

查看:112
本文介绍了CouchDB相当于Sql NOT IN吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找与以下LinQ查询等效的CouchDB JS视图:

I'm looking for the CouchDB JS view equivalent of the following LinQ query :

    var query =
        from f in context.Feed
        where !(from ds in context.DataSource
                select ds.Feed_ID)
        .Contains(f.ID)
        select f;

数据源在其中具有Feed的外键.

Where DataSources have a foreign key to Feeds.

总而言之:获取所有与数据源无关的提要

In a word : get all Feeds not associated with a DataSource

谢谢

推荐答案

您可以使用查看排序规则加入地图中的供稿和数据源:

You can use the view collation to join feeds and data sources in map:

function(doc) {
  if (!doc.type) return;
  if (doc.type == "feed") emit(doc._id, null);
  if (doc.type == "ds" && doc.feed) emit(doc.feed, null);
}

并减少以过滤那些具有链接到其数据源文档的提要ID.例如.使用内置 _count并使用

and reduce to filter those feed ids which have data source documents linking to them. Eg. use of the build-in _count and query with group_level:

http://127.0.0.1:5984/test/_design/join/_view/not_in?group_level=1

对于数据库:

{"id":"1", "type":"feed"}
{"id":"2", "type":"feed"}
{"id":"3", "type":"ds", "feed":1}
{"id":"4", "type":"ds", "feed":1}}

会给您:

{"rows":[
{"key":"1","value":3},
{"key":"2","value":1}
]}

>1是从数据源获得参考的那些feed文档.要获取不带数据源的纯提要列表,您可以在客户端或列表中使用value>1省略记录功能.

Values >1 are those feed docs which have reference from data sources. To get pure feed list w/o datasources you can omit records with value>1 in client or list function.

具有列表功能:

function(head, req) {
  var row;
  while (row = getRow()) {
    if (row.value == 1)
      send(row.key + "\n");
  }
}

并查询:

http://127.0.0.1:5984/test/_design/join/_list/not_ds/not_in?group_level=1

在没有数据源引用的情况下,您将获得带有Feed文档的最终结果.它是带有ID列表的纯文本,您也可以将其格式化为JSON数组.

You will get the final result with feed documents with out reference from data sources. It is plaint text with list of ids, you can also format it for JSON array.

这篇关于CouchDB相当于Sql NOT IN吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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