multiple key ranges as parameters to a couchdb view

Posted by kolosy on Stack Overflow See other posts from Stack Overflow or by kolosy
Published on 2009-09-23T21:44:09Z Indexed on 2010/03/24 0:13 UTC
Read the original article Hit count: 809

Filed under:

is there a way to send multiple startKey/endKey pairs to a view, akin to the keys: [] array that can be posted for keys?

the underlying problem - let's say my documents have "categories" and timestamps. if i want all documents in the "foo" category that have a timestamp that's within the last two hours, it's simple:

function (doc) {
  emit([doc.category, doc.timestamp], null);
}

and then query as

GET server:5894/.../myview?startKey=[foo, |now - 2 hours|]&endkey=[foo, |now|]

the problem comes when i want something in categories foo or bar, within the last two hours. if i didn't care about time, i could just pull directly by key through the keys collection. unfortunately, i have no such option with ranges.

what i ended up doing in the meantime is rounding the timestamp to two-hour blocks, and then multiplexing the query out:

POST server:5894/.../myview
keys=[[foo, 0 hours], [foo, 2 hours], [bar, 0 hours], [bar, 2 hours]]

it works, but will get messy if i want to go back a large amount of time (in relationship to the blocksize)

© Stack Overflow or respective owner

Related posts about couchdb