Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching by key in Apache CouchDB

Tags:

php

nosql

couchdb

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 image 942
Nate Avatar asked May 27 '10 16:05

Nate


People also ask

Is CouchDB key value?

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.

What language do you use to query for data in CouchDB?

CouchDB uses HTTP protocol for API. It uses javascript as its query language to transform the documents and JSON to store data.


3 Answers

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:

  • Finding your data with views in the book (I prefer this resource)
  • Introduction to CouchDB views in the wiki

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.

like image 139
JasonSmith Avatar answered Sep 23 '22 08:09

JasonSmith


http://sitr.us/2009/06/30/database-queries-the-couchdb-way.html

like image 30
Toby Avatar answered Sep 27 '22 08:09

Toby


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.

like image 23
Jaseem Avatar answered Sep 27 '22 08:09

Jaseem