Couchdb View does not re-index

904 views Asked by At

I have a view which looks like this:

function(doc) {
    if (doc.type === 'article' && (Date.parse(doc.published) < (Date.now() - 30 * 60 * 1000))) {
        emit(doc._id, doc._rev);
    }
}

The view basically emits articles that are stale (i.e. {{published date}} < {{present - 30 minutes}}.

Now, the issue is as follows: The view does not update itself after the first read. The first access builds the view on all documents as expected. But thereafter it seems like it only updates itself on change (delete, create or update of new documents).

This is however an issue and not what I desire. I have other articles that are getting stale as time progresses therefore I would like couch to return these articles too but since they are not changed they don't come up in the view.

This, what I just described, seems to be expected couchdb behavior (?). But, is there a way to show aging artciles too ?

PS: An easy way to test this is to insert a document with published=Date.now() and type="article" and run this view. After 30 minutes you will see the document is actually stale as per the view definition but it will not show up in the view. 30 minutes is just a number. You can reduce it to a smaller time frame if you want. Thanks in advance for your help !

1

There are 1 answers

0
Guy On

Yes, this is by design. The view index is only updated on the Create Update and Delete parts of CRUD operations.

Filtering by something dynamic during a Read is done with a key.

In your case you would probably want to emit as follows:

emit(doc.published, doc);

Then in your call to CouchDB you would add parameters that would further filter on the published date.