Search code examples
couchdbcouchbase-litecouchdb-futon

CouchDB boolean filtering


I am trying to work with CouchDB filtering but I can not understand how it works

So lets say, like the example they have:

function(doc, req){
    // we need only `mail` documents
    if (doc.type != 'mail'){
        return false;
    }
    // we're interested only in `new` ones
    if (doc.status != 'new'){
        return false;
    }
    return true; // passed!
}

I am bit confused because if I want to return only

mail documents

I think I should implement something like:

 if (doc.type == 'mail'){
        return true;
    }

At the end we have

return true //passed

but, is that mean that I will return all the documnents I have?


Solution

  • The example doesn't just filter for mail documents, it also requires they be new. Without the "new document check" you could just as well write the code as

    function(doc, req){
      // we need only `mail` documents
      if (doc.type == 'mail') {
        return true;
      }
    
      return false;
    }
    

    The whole logic of the original, though, is implementing (in pseudo-code)

    if (!mail document or !new document) then false
    else true