CarCrazyBen
CarCrazyBen

Reputation: 1136

CouchDB how to exclude documents from View

I have a CouchDB View to return all documents meeting a certain criteria. I have also run "all_docs" against this same DB. In this particular case, my View returns 845 docs while all_docs returns 1127 docs.

Is there a way to code a View to effectively do the inverse, and return all documents NOT meeting my specified criteria? since my DB contains 1127 total docs and my View contains 845 docs, how do I identify the 282 difference?

Upvotes: 1

Views: 320

Answers (1)

Alexis Côté
Alexis Côté

Reputation: 3690

You can either have two views or one single view.

Single view(persons)

function(doc){
  var matchMyCriteria = doc.type ==="person";
  emit(matchMyCriteria);
}

If I want every document document that are of type "person", I query:

_design/docname/_view/by_person?key="true"

If I want the every other documents

_design/docname/_view/by_person?key="false"


Two view(persons)

persons

function(doc){
  if(doc.type == "person")
     emit(doc.id);
}

not_person

function(doc){
  if(doc.type != "person")
     emit(doc._id);
}

Upvotes: 1

Related Questions