0

When I query some view in Couchbase using user_id(key), limit(10) and skip(0) parameter, I get the response that has following structure:

{
  "total_rows":1896,
  "rows":[...]
}

Here is my view who return list of reports based on user_id:-

function map(doc, meta) {
    if (doc.type == 'report' && doc.subscribed) {
        for (var subscriber in doc.subscribed) {
            emit(subscriber, doc);
        }
    }
}

here is the sample report doc:-

{
   "agree_allowed":true,
   "assigned_by":"",
   "assigned_to":"",
   "closed":[

   ],
   "comments_allowed":true,
   "details":"Test",
   "email":"",
   "status":"In Progress",
   "subscribed":{
      "user_cfd29b81f0263a380507":true,
      "user_cfd29b81f0263a380508":true,
      "user_cfd29b81f0263a380509":true,
      "user_cfd29b81f0263a3805010":true
   },
   "summary":"Test",
   "time_open":0,
   "timestamp":"2015-07-17T15:34:30.864Z",
   "type":"report",
   "user_id":"user_cfd29b81f0263a380507",
   "username":"test17"
}

but rows count is 3, so if i want to achieve pagination how can i get total count, So it can be useful for pagination.

rash111
  • 1,307
  • 4
  • 19
  • 35

1 Answers1

1

total_rows contains the unfiltered count, so it should be equal to the total number of subscribed entries (all the [user,report] pairs).

if you want to do efficient pagination, you shouldn't use skip(n), because it forces the index to scan the n first rows and discard them.

what you should do instead is use startkey, startkey_docid and limit (well, actually you would use skip(1) to get rid of the last result of the previous page).

this is explained in quite a bit of detail in this blog post: http://blog.couchbase.com/pagination-couchbase

Simon Baslé
  • 27,105
  • 5
  • 69
  • 70