0
votes

So I am using this approach on CouchDB docs to perform pagination.

  • Request rows_per_page + 1 rows from the view
  • Display rows_per_page rows, store + 1 row as next_startkey and next_startkey_docid
  • As page information, keep startkey and next_startkey
  • Use the next_* values to create the next link, and use the others to create the previous link

One thing I don't understand is, how do I perform sorting using this approach, assuming each document have a last updated timestamp and I want to sort using that field instead of sorting using ids.

1

1 Answers

0
votes

First of all, sorting will always be on the KEYS. Querying _all_docs result by query a table where the key is the _id.

[
  {
    "key": "my_first_id",
    "value": {}
  },
  {
    "key": "my_second_id",
    "value": {}
  }
]

So if you want to sort on another field than _id, you will need to use Map/Reduce(Views) For example, you could create a view where the key is the updatedAt field. This would result in something like this :

[
  {
    "key": "1475858068",
    "value": {}
  },
  {
    "key": "1475553268",
    "value": {}
  }
]

So using the sort would result by sorting the key :)