3

I have 5680 documents in my CouchDB.

I reduced it with something like:

function(doc) {
  if (doc.address.country && doc.cats) {
     for (i = 0; i < doc.cats.length; i++) { 
       emit([doc.address.country, doc.cats[i].id], doc);
     }    
  }
}

Running my view with: myview?key=["CC","category"]

Returns the expected result (25 items).

However there is a field in the response:

{"total_rows":5680,"offset":5655, ...

The total_rows count is my full data set, not only the ones which matched my query. Is there a way to see the matching rows in the result?

I would like to prevent duplicating the map code to just add a reduce function. In addition I would like to prevent two http requests just for that information.

For me this information would be useful to implement something like pagination.

I have read this: http://docs.couchdb.org/en/1.6.1/couchapp/views/pagination.html

But they are not paginating over a limited subset of my data rows.

As I found out, simply substracting offset from the total rows will not work (of course).

How can I have a correct number of total_rows in my response, or what are the best practices in my use case?

Thanks!

Kerr
  • 2,792
  • 21
  • 31
Christian
  • 7,062
  • 5
  • 36
  • 39
  • Hello, I know it`s been a while since you have asked this question but did you find a workaround about this issue ? – rematnarab Nov 23 '17 at 12:15

1 Answers1

0

The total_rows value is the total number of rows in the view. If the view shows all the rows in the database, then that will be the same value. You are correct that it is not the number of rows returned in that query. The number of rows returned in the query is not presented as a separate value, however is is trivially found by getting the length of the returned rows array.

Kerr
  • 2,792
  • 21
  • 31
  • 2
    Hi @Kerr, do you haven an idea how can I archive this when I use paging? In this case the number of rows in the array are the same as the number of results per page. How can I get the total result count of my query (without sending another query)? – Dirk Feb 27 '17 at 17:13
  • I don't understand your question. Are you saying the total_rows value is not what you expect? It's probably best to ask a new question setting out what you are doing, what you are expecting / wanting and what you are seeing. – Kerr Feb 27 '17 at 17:23
  • 1
    Hi @Kerr, What Dirk is asking is how to get the count of total docs that would be emitted from the map with a filtered search if the skip and limit wasn't used without sending a separate request to do the reduce. The expectation is that in addition to the total_rows that shows number of all documents that are in the view, there could be an another property showing number of docs when the key is used as a filter. I have the same problem, I'm using start and end key and skip and limit and can't figure out total number of docs that would be returned by the view without taking limit into account. – woj.sierak Mar 04 '17 at 11:47