开发者

couchdb query map view and not reduce view

开发者 https://www.devze.com 2023-02-14 12:21 出处:网络
How do I query just the map of a view with both a map and reduce using evently? Here is what I have so far in data.js:

How do I query just the map of a view with both a map and reduce using evently?

Here is what I have so far in data.js:

function(data) {
  var numRows = data.rows.map(function(r) {
    return r
  }); //this is returning an object that is the result of the reduce function
      // but I want the total_rows of the map function 

  var sum = data.rows.reduce(function(r) {
    return r.value
  }); //this returns the sum of all row values
  var avg = sum.value / numRows
  $.log(avg);
  $.log(numRows);
  return {'total': numRows, 'average':开发者_运维技巧 avg}
};

I want this to return the total number of rows from the query and the average of their values.

Thanks in advance for the help.


I do not have couchapp at hand right now to test, but I think you are confusing JavaScript map function with CouchDB view map function. Doing data.rows.map() you are calling the Array.map function on the data.rows array. What you are looking for should be in data.total_rows.

Read this tutorial for more information.

UPDATE: the number of rows is data.rows.length.

0

精彩评论

暂无评论...
验证码 换一张
取 消