3

I have a document in CB which has two dates, a start date and an end date. Let's say, a product's price discount. 10% off starting from today and ends next Friday. How can I get all the documents from CB which have a valid discount today?

I made a view and have the following in it:

var dt = new Date();

Which gets today's date. Then I can do a simple

if(doc.FromDate < dt && doc.ToDate > dt){ emit([ ..... ]);

This filters the documents how I want. But...

Question

Is this a good approach re view and index updating? Will the index update every day because the date changed? Just trying to understand the working of CB in this respect

What is the best approach for this type of searching? If not possible please tell me!

Cheers

Robin

Note: Please note, the question is NOT like this here or this here

Community
  • 1
  • 1
Robin Rieger
  • 1,204
  • 1
  • 16
  • 37

2 Answers2

5

Let's me clarify something here:

  • the map() function is used to create/update the index on disk, and this occurs just "after" the document is saved on disk. This is why using date.now() in the map reduce does not really makes sense.

  • So what you will do is to emit the date for example emit( dateToArray(doc.startDate) );

  • then when you query the view(index) you can use the startkey & endkey to do a range query.

&startkey=[2013,4,16]&endkey=[2013,4,24]

Tug Grall
  • 3,410
  • 1
  • 14
  • 16
  • hey, thanks for that. Yeah I completely forgot re the date.now that it is 'static'. My bad.! I guess the only way of doing it is having two values in the emit -- > emit[(doc.from, doc.to), null]. What is the 'smallest' (most in the past) date I can use. I.e. The 'from date' can be any date yesterday or before. I need all records where today's date >= from date. I can't pass [null, 2013/4/5] because the null would get me everything right and ignore the end key? working on the left to right principle re keys. – Robin Rieger May 06 '13 at 21:09
  • Not sure to understand the question but the Date you are using are simply Javascript dates, so you can use what is define here :https://developer.mozilla.org/en/docs/JavaScript/Reference/Global_Objects/Date – Tug Grall May 14 '13 at 10:10
2

the index won't be updated just because system date changed, you have to update the document. also view indexer doesn't allow you to pass any arguments defined by user. in this case you should emit the data and use date as a part of the key to filter on view query. I guess the same behaviour for SQL indexes too. you cannot predict when exactly this document will indexed, in your example you are freezing timestamp when the doc has been indexed, it isn't even the time when it was changed

avsej
  • 3,822
  • 4
  • 26
  • 31
  • 1
    Hey, thanks for the reply. I am a bit confused still. How can I get the documents then which have a date 'range' which includes today? I can't use the start and end key because today won't necessarily match the 'from' and 'to' dates. So your suggestion in this respect won't work right? – Robin Rieger May 05 '13 at 00:42