2
votes

Here is the doc "schema":

{
    type: "offer",
    product: "xxx",
    price: "14",
    valid_from: [2012, 7, 1, 0, 0, 0]
} 

There are a lot of such documents with many valid dates in the past and the future and a lot of times two or three offers in the same month. I can't find a way to make the following view: Given a date, give me a list of the products and their running offer for that date.

I think I need to emit the valid_date field in order to set the endkey of the query to the given date and then I need to reduce on the max of this field, which means i can't emit it.

Have I got it wrong? I am totally new to the map/reduce concept. Any suggestions on how to do it?

1
why not u use timestamp for date field? and is pretty straight forward for range (startkey, and endkey) - ajreal
Yes I could. I dont have strong opinion, i just though it would make it easier to manipulate it. But it doesnt change my problem I think. - Paralife

1 Answers

0
votes

I'm really thrown by your comments about wanting to reduce, based on your requirements you want just a map function - no reduce. Here's the map function based on what you asked for:

function(d) {
  if( d.type === 'offer' ) {
    var dd = d.valid_from;

    dd[1] = ( '0' + ( dd[1] + 1 )).slice(-2);   // remove the +1 if 7 is July not August
    dd[2] = ( '0' + dd[2] ).slice(-2);

    emit( dd.slice(0,3).join('-') );
  }
}

Then to show all offers valid for a given day you'd query this view with params like:

endkey="2012-08-01"&include_docs=true