Note that there are some explanatory texts on larger screens.

plurals
  1. POCouchDB Simple SELECT the right way?
    text
    copied!<p>I am building a very simple users system on Apache CouchDB. I have build a simple view to search users by email:</p> <pre><code>by_email map function(doc) { if (doc.kind == "user" &amp;&amp; doc.email) emit(doc.email, doc.name); } </code></pre> <p>this returns</p> <pre><code> {"id"=&gt;"00006a80-723b-012f-6b38-1040f398478e", "key"=&gt;"spiderman@spiderman.ai", "value"=&gt;"Spiderman"} </code></pre> <p>Now I might be missing something very stupid (if this is the case I do I apologize) but why you can only retrieve 2 fields (3 if we count the ID) from this view ?</p> <p>Like in a normal situation you might want to search your users database starting from the email or user ID and retrieve the full document.</p> <p>Now I did read the example at <a href="http://guide.couchdb.org/draft/cookbook.html" rel="nofollow">http://guide.couchdb.org/draft/cookbook.html</a> (look up by key) and, unless I am missing something very trivial, the result there is incomplete too as it will return only the name of users with a certain age and nothing more. </p> <p>I fail to understand how this would make of a proper use: if you query your db of clients, suppliers, whatever you will want the full document back and not just 1 of the values or 2. </p> <p>So my solution was another view:</p> <pre><code>mailplus map function(doc) { if (doc.kind == "user" &amp;&amp; doc.email) emit(doc.email,doc) } </code></pre> <p>so to return me the email but also the full document as value. This works but as you can imagine it is considerably slower in a bigger database. </p> <p>So the question is: is there any better way to do a simple query to retrieve full details about a user searching by email for example using CouchDB - NoSQL ?</p> <p>I know that the temptation to answer "this is wrong as you can't think SQL in NoSQL" but wait: in a real world application, thinking as NoSQL in terms of Documents and not tables/rows etc, you might want the full document for a user. Imagine you have a simple system to retrieve client details. You need the full document. You do not need to know the id and key only when you are supplying the key, email, in the first place.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload