0

I have a activity bucket in my couchbase-db and I need to retrieve the latest document for different types, My initial approach was this:

Document Format : [ id , { val.time ,val.type, val.load } ]

And then I wrote different views to Map a specific val.type and I used reduce to get the latest val.time, However I have the problem of views not being updated ( Cause apparently the Map is only called on new or changed documents and this approach needs all of the documents to be mapped and reduced.)

What is the best practice / approach for time-based data on Couchbase ( NoSQL ) databases ?

Community
  • 1
  • 1
user1470618
  • 147
  • 2
  • 11

2 Answers2

2

You can access the documents by time with a simple view like this:

  1. Convert your time to an integer value. Eg using the parse() method. http://www.quackit.com/javascript/javascript_date_and_time_functions.cfm

  2. Use this integer value as the key to your view. You can then access the first or last document.

  3. If you always need the last document it's faster to negate the time value, so the largest times are at the start of the index.

Hans
  • 2,800
  • 3
  • 28
  • 40
  • Thanks for your answer DHK, I think the main problem is that my view will work for few documents < 3 , and as I add documents to my bucket the view does not update anymore. Putting stale=false, shows that I am getting a type error "Cannot read property X of null" . My map function ( No reduce as this point.) is checking for undefined fields : The map function goes like this. function (doc, meta) { if ( doc.type === "task-manager" && doc.time ) { emit(parseInt(doc.time),doc); } }. I am totally lost. – user1470618 May 20 '14 at 18:42
1

If you are using a development design document, it is expected that adding new keys into your bucket may have no effect on the view. Since only a subset of keys from the bucket are going into the Map/Reduce pipeline, adding a key that is not going into the subset would trigger no update to your view.

Either evaluate it on a production design document (by clicking the "publish" button), or try adding more keys.

Mickaël Le Baillif
  • 2,027
  • 16
  • 22