1

Currently, my database has three documents, and it looks like this:

{"total_rows":3,"offset":0,"rows":[

{"id":"hello-world","key":"2009/01/15 15:52:20","value":"Hello World"},
{"id":"biking","key":"2009/01/30 18:04:11","value":"Biking"},
{"id":"bought-a-cat","key":"2009/02/17 21:13:39","value":"Bought a Cat"}

]}

I want to query by the specific value "Biking" using the following command:

C:\Users\Alex>curl http://127.0.0.1:5984/alex/_design/example/_view/byDate?value="Biking"

This should return the only row with the value "Biking" but instead it returns the same result as shown above. I am doing all this on Windows. What could I be doing wrong?

1 Answer 1

1

Views are indexed by key field, it's not possible to query them by value one. In your case you need to create another view where data from value field would be emitted as keys.

curl http://127.0.0.1:5984/alex/_design/example/_view/byName?key=""Biking""

P.S. Also note, that key query parameter should contains valid JSON value, so on Windows, you need to set double quotes around the key parameter.

6
  • Oooo yea, I remember now. But I've tried querying by the key, and using double quotes and I get an error..I believe because there are spaces in my date string..How do I escape space characters in Windows? Apr 21, 2014 at 17:16
  • Not sure since I hadn't Windows right by now to test, by try to wrap whole url with quotes like: "http://127.0.0.1:5984/alex/_design/example/_view/byName?key=""foo bar""". Also, url escaping via %20 should works.
    – Kxepal
    Apr 21, 2014 at 17:29
  • So it works when I add the two extra quotes around the string and url, and add %20 in place of the the space between the date and time. The output says "{"total_rows":3,"offset":1,"rows":[ " Do you understand why the total number of rows is 3 even though there's only one row in the query result? Apr 21, 2014 at 18:29
  • total_rows is the total amount of rows in view without applying any filtering conditions. offset means the position from the beginning where the result rows starts from.
    – Kxepal
    Apr 21, 2014 at 19:09
  • Thank you so much. I really appreciate your help. I do not want to start a new topic, since this question will be related to CouchDB as well. So I will be doing a presentation on CouchDB, which is a relatively new topic to me. As I was researching on this topic, I've come across the following quote, "The major benefit of using a document database comes from the fact that while it has all the benefits of a key/value store, you aren’t limited to just querying by key." So can you give an example in CouchDB where this applies? Apr 21, 2014 at 22:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.