Question

function (doc) {
  if(doc.jsonType == 'cart') {
    if((Date.parse(doc.updated_at) + (15 * 60000)) >= Date.now()) {
      for(item in doc.items) {
        emit(item, doc.items[item].quantity);
      }
    }
  }
}

With this function I get the documents that have been updated within the last 15 minutes, for example 2, with quantity 1 and 2, which should result in 3 as a sum. However, when I use the built-in reduce function _sum, I get 32 (sum of all existing rows).

I updated the views several times, but still don't get the correct results.

What's wrong?

Was it helpful?

Solution

It's wrong the use of Date.now() in map (and reduce) functions. Maps/reduce results are saved on disk for fast retrieval at query time. The value of Date.now() you get, is that of when the map was built, not when it's queried. You can find some introductory material on how CochDB's maps work on the wiki and The Definitive Guide.

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