Couchbase document dates search - DateTime.Now()

荒凉一梦 提交于 2019-12-12 14:07:34

问题


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


回答1:


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




回答2:


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]



来源:https://stackoverflow.com/questions/16372149/couchbase-document-dates-search-datetime-now

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!