I have been attempting to retrieve couchdb documents with a date key, of the format: yyyy mm dd.
I’d like to be able to retrieve a range of dates, using views in couchdb-python. I can get it to work, using curl. E.g.:
curl -X GET mylocation:5984/small/_design/Common/_view/date?startkey='"2012%2006%2004"'\&endkey='"2012%2006%2006"'
However, using the view method in Python, e.g.
a=ui.db.view(docname, startkey='"2012%2006%2004"', endkey='"2012%2006%2006"')
a.rows
produces nothing. I’ve tried:
-
Using the startkey alone –works
-
Endkey alone — doesn’t work.
-
Changed the keys to a composite key of the form: “yy”,”mm”,”dd”
then tried retrieving them as follows:
a=ui.db.view(docname, startkey='\["12","06","04"\]', endkey='\["12","06","06"\]')
a.rows
Again, startkey by itself works, no use of endkey works. This also works normally in curl, e.g.:
curl -X GET mylocation:5984/small/_design/Common/_view/date?startkey='\["12","06","04"\]'&endkey='\["12","06","06"\]'
I then changed the keys to be integers rather than strings–again anything with startkey works, anything with endkey doesn’t work.
I finally changed the key to just integers 1–>10, and still can’t get endkey to do anything.
I’m using couchdb-python version 0.8. I’m debugging this inside of eric4 (Could THAT be the problem?)
You don’t need to encode startkey/endkey query parameters explicitly to JSON in view call. CouchDB requires them to have valid JSON value by default. E.g. if your view returns composite key in form [“yy”, “mm”, “dd”], then your view request will be next
a = ui.db.view(docname, startkey=["12","06","04"], endkey=["12","06","06"])
Note, that startkey and endkey has list type values – same as your view function key.
About curl request: it is not equivalent to your db.view() request: single quotes acts as shell string escaping, but they wouldn’t pass with request. So actual request to CouchDB from curl is
mylocation:5984/small/_design/Common/_view/date?startkey=["12","06","04"]&endkey=["12","06","06"]
Unless you’ll have invalid_json error response, because JSON strings should be double quoted.
Anyway, you may look into CouchDB logs (info level is enough for your case) to compare actual requests to CouchDB from your Python program and from curl – they would be different for your situation.