node.jscouchdbcloudant

Get PartitionedList for partition with more than 2000 documents


I have a partitioned Cloudant database (on the free tier) with a partition that has more than 2000 documents. Unfortunately, running await db.partitionedList('partitionID') returns this object:

{
  total_rows: 2082,
  offset: 0,
  rows: [...]
}

where rows is an array of only 2000 objects. Is there a way for me to get those 82 remaining rows, or get a list of all 2082 rows together. Thanks.


Solution

  • Cloudant limits the _partition endpoints to returning a maximum of 2000 rows so you can't get all 2082 rows at once.

    The way to get the remaining rows is by storing the doc ID of the last row and using it to make a startkey for a second request, appending \0 to ask the list to start from the next doc ID in the index e.g.

    db.partitionedList('partitionID', {
      startkey: `${firstResponse.rows[1999].id}\0`
    })
    

    Note that partitionedList is the equivalent of /{db}/_partition/{partitionID}/_all_docs so key and id are the same in each row and you can safely assume they are unique (because it is a doc ID) allowing use the unicode \0 trick. However, if you wanted to do the same with a _view you'd need to store both the key and id and fetch the 2000th row twice.