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

 

Questions


I have documents with fields ‘date’ and ‘group’. And this is my view:

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

What would be the equivalent query of this:

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

This simple solution is not coming into my head. 🙁

 

 

————————————————-

Answer

Pankaj, switch the order of the key you’re emitting to this:

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

Then you can pass in a start key and an end key when querying the view. It might be easier to do a separate query for each group that you want to pull data for. The data will be sorted by date.

I’m on my way out at the moment, but can elaborate more when I get back if it isn’t clear.

couchdb,map,mapreduce,view

Facebook Comments

Post a comment