Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CouchDB Find One not working

Tags:

couchdb

I am a couchDB newbie and am doing the examples in the O'Reilly CouchDB guide.

I have a problem using a view to retrieve a document by key:

curl http://127.0.0.1:5984/basic/_design/example/_view/by_date?key="2009/01/15 15:52:20"

gives the reply:

curl: (52) Empty reply from server

but just retrieving all rows:

curl http://127.0.0.1:5984/basic/_design/example/_view/by_date

gives me 3 rows including the specific row I am looking for:

{"id":"hello-world","key":"2009/01/15 15:52:20","value":"Hello World"}

why doesn't the key query work?

I am using CouchDB version 0.10.0 on Ubuntu 9.10

like image 245
Andrew Avatar asked Mar 27 '10 17:03

Andrew


2 Answers

CouchDB expects the start_key parameter to be a valid JSON-compatible type, such as "a string" or 12345 or ["an", "array", "with", 5.0, "elements"]. If you check your CouchDB logs you will probably see a 400 (bad client request) entry because your key is either invalid UTF8 or invalid JSON.

You probably have two problems:

  1. The shell is interpreting your quotes which must actually be sent to CouchDB. Try single-quoting your double-quote string.

  2. You probably also need to encode your key so that it is a valid URL. Specifically, replace your space with %20

Putting this all together, the following works for me on CouchDB 0.11 on Ubuntu 9.10.

$ curl http://127.0.0.1:5984/blog/_design/docs/_view/by_date?key='"2009/01/30%2018:04:11"'
{"total_rows":1,"offset":0,"rows":[
{"id":"biking","key":"2009/01/30 18:04:11","value":"Biking"}
]}
like image 75
JasonSmith Avatar answered Oct 05 '22 22:10

JasonSmith


It worked, I single-quoted the key string and encoded the space character so the request became:

/by_date?key='"2009/01/30%2015:52:20"'
like image 38
user303608 Avatar answered Oct 05 '22 21:10

user303608