简体   繁体   English

Couchbase与Cloudant CouchDB-在减少方面与Javascript的区别

[英]Couchbase vs Cloudant CouchDB - differences with Javascript in reduce

Ok, I've been banging my head for a way around this all weekend, and I'm finally stumped. 好的,这整个周末我一直在想办法,我终于陷入了困境。 My reduce map func: 我的reduce map func:

function(doc) {
 if(doc.primary_keys) {
    for(pi in doc.primary_keys) {
        var pk = doc.primary_keys[pi];

        for(row_i in doc.data) {
            var row = doc.data[row_i];
            if(row[pk]) {
                emit([row[pk]], doc._id);
            }
        }
    }
 }
}

And my view reduce: 我的看法减少了:

 function(keys, values) {
   var retval = {};

   for(var val_i in values) {
       var key = values[val_i];

       if(key in retval) {
           retval[key] += 1;
       } else {
           retval[key] = 1;
       }
    }

    return(retval);
 }

This is what my local (Couchbase) CouchDB returns: 这是我的本地(Couchbase)CouchDB返回的内容:

key      value
["a"]    {test2: 1, test: 1}

But here's Cloudant returns: 但是这是Cloudant的回报:

key      value
["a"]        {[object Object]: 1, [object Object]: 1}

I've suspected some js env difference, but every workaround solution leads me to the same issus; 我怀疑js env有所不同,但是每个解决方法都将我引向同一个问题。 Cloudant's value for values[val_i] is Object type, where I'd expect a string. Cloudant的values [val_i]值是对象类型,我希望在其中输入字符串。 I have no idea why. 我不知道为什么。 I'm pretty open to re-reduce at this point if it helps. 如果有帮助的话,我现在很乐意重新降低。

Why exactly are these different??? 为什么这些完全不同???

First up you need to specify how your reduce function works in a rereduce case ( http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views has info). 首先,您需要指定在减少情况下减少功能的工作方式( http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views包含信息)。 On Cloudant the rereduce is always called (to collect data from the various shards into the final view result). 在Cloudant上,总是调用rereduce(将各种分片中的数据收集到最终视图结果中)。 That's why you get the response you see. 这就是为什么您得到看到的答复的原因。

Second, it looks like you're trying to get a count of data by primary_key. 其次,您似乎正在尝试通过primary_key获取数据计数。 In general you want to use the built in _sum function for that - it's faster than custom js code, and _sum works over objects in Cloudant. 通常,您要为此使用内置的_sum函数-它比自定义js代码更快,并且_sum对Cloudant中的对象起作用。

Third, the unreduced view will contain the doc._id automatically, so emit a 1 instead to make the _sum work. 第三,未缩减的视图将自动包含doc._id,因此发出1来使_sum有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM