0

好吧,我整个周末都在想办法解决这个问题,我终于被难住了。我的减少地图功能:

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);
            }
        }
    }
 }
}

我的观点减少了:

 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);
 }

这是我的本地(Couchbase)CouchDB 返回的内容:

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

但这是 Cloudant 的回报:

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

我怀疑一些 js env 差异,但每个解决方法都将我引向相同的问题;Cloudant 的 values[val_i] 值是 Object 类型,我希望它是一个字符串。我不知道为什么。如果有帮助,我很愿意在这一点上重新减少。

为什么这些完全不同???

4

1 回答 1

4

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). On Cloudant the rereduce is always called (to collect data from the various shards into the final view result). 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. 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.

Third, the unreduced view will contain the doc._id automatically, so emit a 1 instead to make the _sum work.

于 2013-04-29T10:03:55.810 回答