couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kevin Coombes <>
Subject Re: couchdb data structure
Date Fri, 22 Nov 2013 14:38:19 GMT

On 11/22/2013 9:18 AM, John Norris wrote:
> Hi Jens,
> thanks for this - makes sense.
> To continue on this path, so if I have separate document types, where 
> docTypeA has a reference to docTypeB, can I have a view that returns a 
> docTypeA based on a value within docTypeB object? My feeling from the 
> docs I have seen is "no".
> Also, my view returns a list of key, values. And I can use the eg key 
> parameter to return a particular key, value. I can also use the keys 
> parameter to return more than one particular key. But can I return a 
> document based on more than one field.
> What I mean is if a document type is defined as
> {
>    id: xxxx,
>    fname: fred,
>    lname: smith,
>    dob: 01/01/2001
> }
> The view can emit a key of "doc.fname, doc.lname" but can I search on 
> "fred" and "smith"? I could combine the two and search on "fredsmith" 
> I guess. 
You can define separate "lastname" and "firstname" views, and queries 
would look like


If you are not sure if the first name is "fred" or "frederick", you can 
get them all with a query like


You could also define a view (say 'name') that looks like

function(doc) {
     if(doc.fname) {
         emit(doc.fname, doc._id);
         emit(doc.lname, doc._id);
         emit(doc.fname+' '+doc.lname, doc._id);
         emit(doc.lname+', '+doc.fname, doc._id);

and then you could search on first name, last name, or full name into 
the same view.
> But, moving away from a name, it was two towns, then they could be any 
> order eg "london-leeds" or "leeds-london". Again, the could be 
> combined but can a doc be emitted twice eg emit called twice?
Yes; see the above example.

> Sorry this is all a bit confused, and the examples are not great but 
> hopefully the general question is getting over.
> Regards,
> John

View raw message