Is it possible to search by key value in Apache CouchDB? Given the sample data below (spaced for readability):
{
"_id":"a754a63dcc7f319b02f7ce6de522ca26",
"_rev":"1-5bd88e53fe0869b8ce274b49a2c1ddf5",
"name":"john smith",
"email":"[email protected]",
"username":"jsmith"
}
Could I query the database for the user jsmith or for the user having the email [email protected]? How would I go about this?
Like the LevelDB key-value store, CouchDB can store any binary data that is modeled in chaincode (CouchDB attachments are used internally for non-JSON data). As a document object store, CouchDB allows you to store data in JSON format, issue rich queries against your data, and use indexes to support your queries.
CouchDB uses HTTP protocol for API. It uses javascript as its query language to transform the documents and JSON to store data.
Yes, that is certainly possible. You will create a couple of views, which are sorted lists ("index") of your data, one per key.
Tobias's link is useful. However the standard CouchDB documentation will cover this also:
For example, in your design document, you might want a users_by_email
view, with keys based on the email
field; then a users_by_name
view keyed on the username
field, etc. Experiment with the temporary views in Futon until you get your function working just right, and then store it in your design document permanently.
Good luck!
P.S. There is a way to combine all of these requirements into one view. Briefly, you could key on ["email", "[email protected]"]
or ["name": "john smith"]
however remember, CouchDB is relaxed: The simpler method above will work fine. When you become comfortable with views, you can explore this "collated" style.
http://sitr.us/2009/06/30/database-queries-the-couchdb-way.html
You cannot search by a keyvalue. You can only search by keys.
If you want to search for emails, emit [email, whatever data] in some view and add ?key='search email'
to view url.
Searching only by keys gives huge performance benefits and hence this feature [searching by key value] will never ever come to couchDB.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With