Note that there are some explanatory texts on larger screens.

plurals
  1. POCouchdb map/reduce to return the first in a stream, then order by time
    primarykey
    data
    text
    <p>I have a couchdb which holds a series of events. Each event has an owner, an id, a time it occured and a message (plus a bunch of other stuff which doesn't matter for this exercise). I'd like a list of events which occured recently ordered by time. I looked through this question <a href="https://stackoverflow.com/questions/5198023/couchdb-filter-latest-log-per-logged-instance-from-a-list">CouchDB - filter latest log per logged instance from a list</a> and tried using it with the comparison in the reducer flipped to keep the first message (using the form where I have a complex key).</p> <p>Unfortunately it doesn't quite seem to do what want.</p> <p>Here's my map function</p> <pre><code>function(doc) { var owner, id; if (doc.owner &amp;&amp; doc.stream_id &amp;&amp; doc.message &amp;&amp; doc.receipt_time) { emit([doc.owner,doc.stream_id,doc.receipt_time], { owner: doc.owner, stream_id: doc.stream_id, timestamp: doc.receipt_time, message: doc.message }); } } </code></pre> <p>and my reduce function</p> <pre><code>function(keys, values) { var challenger, winner = null; for (var a = 0; a &lt; values.length; a++) { challenger = values[a]; if (! winner) { winner = challenger; } else { if (winner.owner !== challenger.owner &amp;&amp; winner.trace_id !== challenger.trace_id ) { return null; } else if (challenger.timestamp &lt; winner.timestamp) { winner = challenger; } } } return winner; } </code></pre> <p>Then I invoke with ?descending=true&amp;group=true&amp;group_level=2 to get the first message from each stream, however, it doesn't seem to be ordered by time, but by owner and stream_id, like this</p> <pre><code>{"rows":[ {"key":["sam","a"], "value": {"owner":"sam","stream_id":"a","timestamp":1303754236482,"message":"foo"} }, {"key":["sam","b"], "value": {"owner":"sam","stream_id":"b","timestamp":1303752578476,"message":"bar"} }, {"key":["jim","j1"], "value": {"owner":"jim","stream_id":"j1","timestamp":1303625378839,"message":"stuff"} }, {"key":["bob","loblaw"], "value": {"owner":"bob","stream_id":"loblaw","timestamp":1303328396532,"message":"more stuff"} }, {"key":["anthony","foo"], "value": {"owner":"anthony","stream_id":"foo","timestamp":1303769699444,"message":"even more"} } ]} </code></pre> <p>(Notice the final entry actually is the most recent in time).</p> <p>So what I'd like is for the final view to be what it is now but ordered by time. Is there a way to do this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload