减少输出必须更快地缩小-减少到文档列表 [英] Reduce output must shrink more rapidly -- Reducing to a list of documents

查看:76
本文介绍了减少输出必须更快地缩小-减少到文档列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在沙发数据库中有一些带有json的文档,如下所示。每个cid都会改变。我创建了一个具有map / reduce功能的视图,以过滤掉一些文档并返回json文档列表。

I have a few documents in my couch db with json as below. The cId will change for each. And I have created a view with map/reduce function to filter out few documents and return a list of json documents.

文档结构-

{
  "_id": "ccf8a36e55913b7cf5b015d6c50009f7",
  "_rev": "8-586130996ad60ccef54775c51599e73f",
  "cId": 1,
  "Status": true
}

以下是示例地图:

function(doc) {
  if(doc.Key && doc.Value && doc.Status == true)
      emit(null, doc);
}

以下是样本减少:

function(key, values, rereduce){

var kv = [];

values.forEach(function(value){
   if(value.cId != <some_val>){
       kv.push({"k": value.cId, "v" : value});  
   }
});          

return kv;
}

如果有两个文档,并且reduce输出的列表包含一个文档,则此方法有效精细。但是,如果我再添加一个文档(cId = 2),则会引发错误-减少输出必须更快地缩小。为什么会这样呢?以及如何实现我打算做的事情?

If there are two documents and reduce output has list containing 1 document, this works fine. But if I add one more document (with cId = 2), it throws the errors - "reduce output must shrink more rapidly". Why is this caused? And how can I achieve what I intend to do?

推荐答案

错误原因是,reduce函数实际上并没有减少任何事情(它是收集对象)。 文档提到了这一点:

The cause of the error is, that the reduce function does not actually reduce anything (it rather is collecting objects). The documentation mentions this:


B树存储的工作方式意味着,如果您实际上并没有在减少功能中减少
的数据,您最终将拥有CouchDB
复制大量数据,这些数据呈线性增长,如果不是
更快的话,则与您查看的行数有关。

The way the B-tree storage works means that if you don’t actually reduce your data in the reduce function, you end up having CouchDB copy huge amounts of data around that grow linearly, if not faster with the number of rows in your view.

CouchDB将能够计算最终结果,但仅适用于具有几行的视图
。任何较大的视图都会花费可笑的
构建时间。为了解决这个问题,如果您的reduce函数没有减少其输入的
值,则CouchDB从0.10.0版本开始将向
抛出错误。

CouchDB will be able to compute the final result, but only for views with a few rows. Anything larger will experience a ridiculously slow view build time. To help with that, CouchDB since version 0.10.0 will throw an error if your reduce function does not reduce its input values.

我不清楚您打算实现什么。
是否要根据某些条件检索文档列表?在这种情况下,没有reduce的视图就足够了。

It is unclear to me, what you intend to achieve. Do you want to retrieve a list of docs based on certain criteria? In this case, a view without reduce should suffice.

编辑:如果所需结果取决于某个文档中存储的值,则CouchDB具有称为的功能> 列表 。这是一个设计功能,如果您传递include_docs = true,则可以访问给定视图的所有文档。

If the desired result depends on a value stored in a certain document, then CouchDB has a feature called list. It is a design function, that provides access to all docs of a given view, if you pass include_docs=true.

列表URL遵循以下模式:

A list URL follow this pattern:


/ db / _design / foo / _list / list-name / view-name

/db/_design/foo/_list/list-name/view-name

就像视图一样,列表在设计文档中定义:

Like views, lists are defined in a design document:

{
  "_id" : "_design/foo",
  "lists" : {
    "bar" : "function(head, req) { 
       var row;
       while (row = getRow()) {
         if (row.doc._id === 'baz') // Do stuff based on a certain doc
       } 
    }"
  },
  ... // views and other design functions
}

这篇关于减少输出必须更快地缩小-减少到文档列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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