Question

Trying to update an existing CouchDB map function so that it only returns docs created in the past 24 hours.

The current map is very simple

function(doc) {
    if(doc.email && doc.type == 'user') 
    emit(doc.email, doc);
}

I'd like to get the current linux timestamp value and compare that to the creationTime.unix value stored in the doc.

Is that possible?

N.B I'm building the view in futon

Was it helpful?

Solution

I do not know if you can do that, but it if you can that would be very bad for CouchDB database sanity.

Map functions for same document should always emit same values, each time you invoke it (provided that document has not changed in the mean time). This is important since CouchDB stores this emited data in the index, and does not recalculate it again until it is necessary. If map functions could emit different values for the same doc, that would render index unusable.

So, no, do not try that.

Good news is that you can easily achieve what you need without that. If you emit creation time, than you can query your view just for docs with creation time in certain interval like in:

/blog/_design/docs/_view/by_date?startkey="2010/01/01 00:00:00"&endkey="2010/02/00 00:00:00"

Read more how you can query your views in CouchDB The Definitive Guide

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top