Excluding design documents in _all_docs query in Cloudant

1.6k Views Asked by At

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="`"
1

There are 1 best solutions below

2
Glynn Bird On BEST ANSWER

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