0

I'm storing couchDB docs and they have an attribute date which I want to sort by.

{

  ...blahblah...

  "date": [
    2015,
    5,
    16
  ]
}

I can't seem to figure out how to make a view that allows me to supply a startdate=[2015,5,1]&enddate=[2015,5,31] query. I want to get the stuff for May (month 5)

Community
  • 1
  • 1
tim
  • 3,823
  • 5
  • 34
  • 39

2 Answers2

1

It should be as simple as:

function (doc) {
  emit(doc.date);
}

Then your query needs to be: start_key=[2015,5,1]&end_key=[2015,5,31]

Check out the documentation

Dominic Barnes
  • 28,083
  • 8
  • 65
  • 90
  • I knew it was something simple. Although I think it's supposed to be `startkey` and `endkey` without underscore, right? thanks for the link. – tim May 08 '15 at 22:13
  • You can use either, they're aliases for the same thing. (source: [documentation](http://docs.couchdb.org/en/1.6.1/api/ddoc/views.html)) – Dominic Barnes May 10 '15 at 00:59
0

As an alternative, If the date field specified documents are created in order (that is doc with field date = "2015, 4, 16" was created before the doc with field date = "2015, 4, 20). In that scenario, you can also sort using the timestamp when the document was created.

function(doc) { 

emit([doc.created_at,doc.date], doc);
}

http://wiki.apache.org/couchdb/View_collation

akhil gupta
  • 167
  • 1
  • 1
  • 11
  • interesting, except it's not about creation date of the document, but a date stored in it. – tim May 08 '15 at 22:09