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

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

问题描述

创建一个视图,该视图仅返回文档中值的子集,每个值的键和值都包含在 json 字符串中.就像如果一个给定的视图返回一个如下所示的文档,是否可以为一个请求获取一些字段信息?谢谢<代码>{总行数":10,偏移":3,行":[{"id":"doc1",关键":abc123",价值": {"_id":"aaaa","_rev":"bbb","field1":"abc","field2":"bcd","field3":"cde","field4":"123","field5":"789","field6":"aa@email.com","field7":"ttt","field8":"iii",字段9":[{"field91":"tyui",字段 92":55555"}],"field10"::"0000","field11"::"55555",字段 12"::0030"…………}}我只想创建一个仅返回以下某些字段的视图:<代码>{"field1":"abc","field2":"bcd","field3":"cde","field4":"123","field5":"789","field6":"aa@email.com","field7":"ttt","field8":"iii",字段9":[{"field91":"tyui",字段 92":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天全站免登陆