0

I have documents, that look like

{
    ...
    date: '2013-05-25T04:06:20.277Z',
    user: 'user1'
    ...
}

I would need to find documents that have a date within a given range, and a given user. I also need to sort by date.

I have tried emit([doc.user, dateToArray(doc.date)], null) but with this, I cannot sort by date, because AFAIK the key that needs to be sorted on has to be on the left side. Is this correct?

If I try to flip the keys the other way around, no matter what user I put in the startKey & endKey it does not change anything.

for example: startKey: [[0,11,21,0,0,0],"user1"], endKey: [[2016,11,21,0,0,0],"user1"] finds documents from all users, even though I would suppose it to find only documents, where the second key is user1.

How should I do this? My document count can go up to millions, so doing stuff on code-side is out of question..

Mico
  • 1,978
  • 2
  • 13
  • 17

1 Answers1

0

Atleast for now, I ended up having 2 seperate views (byDate and byUserAndDate)

When I need to find only by date I use the byDate view which has the date as the only key, sorting works fine. Also, when I search by particular user I use the byUserAndDate which has [doc.user, doc.date] as its compound key, when the result contains only items for 1 user, the sort obviously works fine because it will sort by user first, and then by date.

Mico
  • 1,978
  • 2
  • 13
  • 17