如何仅凭单个请求从CouchDB中获取某些文档的某些字段? [英] How to get from CouchDB only certain fields of certain documents with a single request?

查看:148
本文介绍了如何仅凭单个请求从CouchDB中获取某些文档的某些字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建一个视图,该视图仅返回文档中值的子集,每个值的键和值都位于json字符串中.就像一个给定的视图按如下所示返回文档一样,是否可以为一个请求获取一些字段信息?谢谢你 { "total_rows":10, "offset":3, "rows":[{ "id":"doc1", "key":"abc123", "value": { "_id":"aaaa", "_rev":"bbb", "field1":"abc", "field2":"bcd", "field3":"cde", "field4":"123", "field5":"789", "field6":"aa@email.com", "field7":"ttt", "field8":"iii", "field9":[{ "field91":"tyui", "field92":"55555" }], "field10"::"0000", "field11"::"55555", "field12"::"0030"......... } } 我只想创建一个只返回以下内容的视图: { "field1":"abc", "field2":"bcd", "field3":"cde", "field4":"123", "field5":"789", "field6":"aa@email.com", "field7":"ttt", "field8":"iii", "field9":[{ "field91":"tyui", "field92":"55555" }] }

create a view that return only a subset of values from a document, each with its key and value within a json string. like if one given view returns a document as this following, Is it possible to get some fields information for a one request? thank you { "total_rows":10, "offset":3, "rows":[{ "id":"doc1", "key":"abc123", "value": { "_id":"aaaa", "_rev":"bbb", "field1":"abc", "field2":"bcd", "field3":"cde", "field4":"123", "field5":"789", "field6":"aa@email.com", "field7":"ttt", "field8":"iii", "field9":[{ "field91":"tyui", "field92":"55555" }], "field10"::"0000", "field11"::"55555", "field12"::"0030"......... } } I just want to create a view that returns some fields only the following: { "field1":"abc", "field2":"bcd", "field3":"cde", "field4":"123", "field5":"789", "field6":"aa@email.com", "field7":"ttt", "field8":"iii", "field9":[{ "field91":"tyui", "field92":"55555" }] }

推荐答案

一种地图函数,仅发出具有选定字段的新文档.例如,让我们仅映射field1(一个字符串)和field9(一个数组):

A map function that emits a new document with selected fields only. As an example, let's map field1 (a string) and field9 (an array) only:

function map(doc) {
  emit(doc._id, {
    field1: doc.field1, 
    field9: doc.field9
  });
}

在上面的示例中,将使用键(原始文档ID)和值(您需要的映射字段)触发每个文档. 如果您打算以后添加reduce函数,这将很有用.

In the above example, each document will be fired with a key being the original doc ID and the value being the mapped fields you require. This is useful if you are planning to add a reduce function later.

根据您的用例,您可能只想发出映射的对象:

Depending on your use case, you may just want to emit the mapped objects:

function map(doc) {
  emit({
    field1: doc.field1, 
    field9: doc.field9
  });
}

请参阅 http://guide.couchdb.org/draft/views.html 建立数据视图的文档非常好,您可以通过实验发现很多内容.

Please see http://guide.couchdb.org/draft/views.html The documentation on building data views is pretty good, you can discover a lot by experimenting..

这篇关于如何仅凭单个请求从CouchDB中获取某些文档的某些字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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