Need a CouchDB trick to sort by date and filter by group

I have documents with the fields 'date' and 'group'. And this is my opinion:

byDateGroup: {
  map: function(doc) {
    if (doc.date && doc.group) {
      emit([doc.date, doc.group], null);
    }
  }
}

What would be the equivalent query:

select * from docs where group in ("group1", "group2") order by date desc

This simple solution does not occur to me. :(

+3
source share
3 answers

Pankaj, switch the order of the key you use for this:

emit([doc.group, doc.date], doc);

You can then pass the start key and the final key when requesting a view. Perhaps it would be easier to make a separate request for each group for which you want to receive data. Data will be sorted by date.

I am on the way now, but I can clarify when I will return, if this is not clear.

+1
source

?

function(doc) {
  if (doc.date && ["group1", "group2"].indexOf(doc.group) !== -1)) {
    emit([doc.date, doc.group], null);
  }
}
0

:

byDateGroup1Group2: {
  map: function(doc) {
    if (doc.date && doc.group && (doc.group === "group1" || doc.group === "group2") {
      emit(doc.date, doc);
    }
  }
}

which you request (presumably in a list function) with the request descending=true.

0
source

All Articles