5

I want to retrieve all the documents other than design documents but _all_docs returns all the documents in the DB. From the answers, I have found that using two queries would give the results.

  1. _all_docs?endkey="_" - lists documents upto the first design doc
  2. _all_docs?startkey="design_\uffff" - lists documents after the design docs

This does not work if the document following the design docs, has _id "``test". It gives the documents having _id beginning with small letters.

The ASCII of _ is 95, and that of backtick is 96. Small letters begin with 97.

So can the above query be modified to:

_all_docs?startkey="`"
Sufian
  • 6,405
  • 16
  • 66
  • 120
ShwetaJ
  • 462
  • 1
  • 8
  • 32

1 Answers1

5

You are quite correct that the _all_docs endpoint does return design documents. As the _ character sits between numbers + uppercase letters and the lowercase letters, the design documents appear just before documents starting with a lower case letter (or a backtick in your example).

This leaves you with two choices:

  1. Make two calls to _all_docs to get the documents "either side" of the design documents:

     GET /mydb/_all_docs?endkey="_"
     GET /mydb/_all_docs?startkey="`"
    
  2. Or, create a new MapReduce view. As MapReduce views do not index design documents, this allows you to get a list of all your documents (excluding design docs) in a single query.

The map function could be as simple as

function(doc) {
  emit(doc._id, null);
}

The query the view with GET /mydb/_design/report/_view/myalldocs

Sufian
  • 6,405
  • 16
  • 66
  • 120
Glynn Bird
  • 5,507
  • 2
  • 12
  • 21
  • can you further explain this approach? I do not understand how to "Make two calls to _all_docs". Thank you! – CarCrazyBen Nov 01 '17 at 17:27
  • @CarCrazyBen First make an API call /_all_docs?endkey="_". This will give you all the documents before Design Documents. Store this result. The make an API call /_all_docs?startkey="`". This will give you all the documents that appear after the design documents. Then, combine the result of the two API calls. In this way you have excluded the design documents to fetch only the actual data documents. If you observe, documents are arranged in Cloudant in the order of ASCII value of the characters of the _id field. – ShwetaJ Apr 04 '18 at 05:54