在CouchDB中执行WHERE - IN查询 [英] Performing a WHERE - IN query in CouchDB

查看:129
本文介绍了在CouchDB中执行WHERE - IN查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过一次调用CouchDB来查询特定文档的列表。



使用SQL我会做类似

  SELECT * 
FROM database.table
WHERE database.table.id
IN(2,4,56);

在CouchDB中通过 _id 或另一个字段?

解决方案

您需要使用视图 c> 查询参数以获取指定集中键的记录。

  function(doc){
emit(doc.table.id,null);
}

然后

  GET / db / _design / ddoc_name / _view / by_table_id?keys = [2,4,56] 

要在同一时间检索文档内容,只需向请求中添加 include_docs = True 查询参数即可。



UPD :可能您可能有兴趣通过此参考ids(2,4,56)检索文档。默认情况下,CouchDB视图映射发出的键与它们所属的文档。要调整此行为,您可以使用链接的文档诀窍:

  function(doc){
emit(doc.table.id,{'_id':doc.table.id});
}

现在请求

  GET / db / _design / ddoc_name / _view / by_table_id?keys = [2,4,56]& include_docs = True 
pre>

将返回 id 字段指向包含 2 4 56 键和 doc 一个包含引用的文档内容。


I would like to query for a list of particular documents with one call to CouchDB.

With SQL I would do something like

SELECT *
FROM database.table
WHERE database.table.id
IN (2,4,56);

What is a recipe for doing this in CouchDB by either _id or another field?

解决方案

You need to use views keys query parameter to get records with keys in specified set.

function(doc){
    emit(doc.table.id, null);
}

And then

GET /db/_design/ddoc_name/_view/by_table_id?keys=[2,4,56]

To retrieve document content in same time just add include_docs=True query parameter to your request.

UPD: Probably, you might be interested to retrieve documents by this reference ids (2,4,56). By default CouchDB views "maps" emitted keys with documents they belongs to. To tweak this behaviour you could use linked documents trick:

function(doc){
    emit(doc.table.id, {'_id': doc.table.id});
}

And now request

GET /db/_design/ddoc_name/_view/by_table_id?keys=[2,4,56]&include_docs=True

will return rows with id field that points to document that holds 2,4 and 56 keys and doc one that contains referenced document content.

这篇关于在CouchDB中执行WHERE - IN查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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