2

I'm storing addresses data in Couchdb, and am looking for a way to get an array of just the values, instead of key: value for every record.

This is the current response:

{"total rows": 2438, "offset": 0, "rows":[
    {"id": "ec5de6de2cf7bcac9a2a2a76de5738e4", "key": "user_298774", "value": {"city": "Milano", "address":"Corso Como, 42b"},
    {"id": "a2a2a76de573ae4ec5de6de2cf7bcac9", "key": "user_276341", "value": {"city": "Vicenza", "address":"Via Quinto Sella, 118"}
    ... (etc).
]}

I only really need:

[{"city": "Milano", "address":"Corso Como, 42b"},
 {"city": "Vicenza", "address":"Via Quinto Sella, 118"},
...]

I really need to minimize the usage of bandwidth that a JSON response consumes. I can't seem to find a way to transform the view into a simple array. Suggestions?

4

1 回答 1

3

您得到的响应符合 Couch 基于 REST 的协议。为了重新格式化它,提供了两种方法:显示函数列表函数。基本思路是一样的,但是第一个适合检索文档,列表功能适合你!

list 函数在服务器内部运行查询,并使用您的 JS 代码发送任意转换的输出。您需要的 API 很简单:

  • getRow()使用该函数从视图中获取每条记录。
  • obj使用.将 JS 对象导出到字符串(包含 JSON)toJSON(obj)
  • 使用 .将输出发送到客户端send(json)

如果带有数据的 map/reduce 视图 URL 是/mydb/_design/myapp/_view/mydocs-by-user并且列表函数名称是mylist重新格式化的结果到带有 URL 的客户端/mydb/_design/myapp/_list/mylist/mydocs-by-user

请参阅上面引用的列表函数文档和指南中的章节以获取长期教程。

于 2013-05-28T10:08:10.480 回答