Couchdb views with multiple keys

953 views
Skip to first unread message

Mathieu Castonguay

unread,
Feb 13, 2012, 1:36:43 PM2/13/12
to ektorp-discuss, jdr...@justlexit.com
I use Ektorp with the @View annotation and create a query with
startKey(), but I realize this question is more about couchdb than
ektorp, I've hit a road block and can't seem to move beyond this, so
any help would be appreciated.

I have a simple document named Order structure with the fields id,
name, userId and timeScheduled.

What I would like to do is create a view where I can find the doc.id
for those who's userId is some value and timeScheduledis after a given
date.

My view:

"by_users_after_time": {
"map": "function(doc) { if (doc.userId && doc.timeScheduled)
{ emit([doc.userId,doc.timeScheduled], doc._id); }}"
}

If I do localhost:5984/orders/_design/Order/_view/by_users_after_time?
startKey="[f98ba9a518650a6c15c566fc6f00c157,2012-01-01T11:40:52.280Z]"
I get every result back. Is there a way to access key[1] to do an if
doc.userId == key[1] or something along those lines and simply emit on
the time?

This would be the SQL equivalent of select id from Order where userId
= "f98ba9a518650a6c15c566fc6f00c157" and timeScheduled >
2012-01-01T11:40:52.280Z;

I did quite a few Google searches but I can't seem to find a good
tutorial on working with multiple keys. It's also possible that my
approach is entirely flawed so any guidance would be appreciated.

Thank you,

Henrik Lundgren

unread,
Feb 22, 2012, 10:58:18 AM2/22/12
to ektorp-...@googlegroups.com
Sorry for your very long wait in the moderation queue but somehow I
missed your message.

If you haven't already solved your problem here's one tip:

The start key parameter should be named "startkey", all lowercase. You
seems to be using "startKey".

Besides that, Ektorp provides a convenience class
org.ektorp.ComplexKey that helps you with creating complex keys.
Take a look at it.

/Henrik

Julien Dreux

unread,
Jun 25, 2012, 4:00:08 PM6/25/12
to len...@his-body.com, ektorp-...@googlegroups.com
See the answer to the same question on stackoverflow: http://stackoverflow.com/questions/9266235/couchdb-view-filtering-by-date/9626287#9626287

"The answer actually came from the couchdb mailing list:

Essentially, the Date.parse() doesn't like the +0000 on the timestamps. By doing a substring and removing the +0000, everything worked.

For the record,

document.write(new Date("2012-02-13T16:18:19.565+0000")); //Outputs Invalid
Date
document.write(Date.parse("2012-02-13T16:18:19.565+0000")); //Outputs NaN

But if you remove the +0000, both lines of code work perfectly."


JULIEN DREUX
www.justlexit.com


On Mon, Jun 25, 2012 at 3:27 PM, <len...@his-body.com> wrote:
Mathieu Did you ever figure out how to do this?  I'm have same issue trying to query with a key and dateRange. An examples or direction appreciated. 
Reply all
Reply to author
Forward
0 new messages