You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by "Welch, Matt" <Ma...@compuware.com> on 2009/10/28 13:43:25 UTC

Can you connect one view to another?

I'm pretty new to CouchDB so please pardon me if this is a silly
question, but can you connect the results of one view to another?

Imagine I have a bunch of children documents and each child has a
favorite color. I also have a bunch of toy documents and each toy has a
color. I want to build a list of what toys each child will like based on
its color. 

I can make a view to emit(color, name) for each toy and something
similar for each child. But then do I have to connect the two in my
code? If your dataset gets large that seems like a lot of work gets
pushed out to the client end. Is there any way to construct a view that
links these together?




The contents of this e-mail are intended for the named addressee only. It contains information that may be confidential. Unless you are the named addressee or an authorized designee, you may not copy or use it, or disclose it to anyone else. If you received it in error please notify us immediately and then destroy it.


Re: Can you connect one view to another?

Posted by Daniel Trümper <tr...@googlemail.com>.
Hi,

> I'm pretty new to CouchDB so please pardon me if this is a silly
> question, but can you connect the results of one view to another?
Do you mean something like chaining the views? Then I guess the  
preferred way is to store the view result as documents again. The  
second view could then use these documents as basis of its computation.

> Imagine I have a bunch of children documents and each child has a
> favorite color. I also have a bunch of toy documents and each toy  
> has a
> color. I want to build a list of what toys each child will like  
> based on
> its color.
>
> I can make a view to emit(color, name) for each toy and something
> similar for each child. But then do I have to connect the two in my
> code? If your dataset gets large that seems like a lot of work gets
> pushed out to the client end. Is there any way to construct a view  
> that
> links these together?
AFAIK this has to be done on the client side. But you could do  
something like this:

map = function(doc) {
   if( doc.type=="TOY" ) {
     emit( [doc.color, "T", doc.name], null );
   } else if( doc.type=="CHILD" ) {
     emit( [doc.color, "C", doc.name], null );
   }
}

Then you could use the view collation and query the view like this:

/db/_design/tmp/toys?startkey=[GREEN]&endkey=[GREEN, "T", {}]

This would give you a list of all toys being green and all children  
with green as their favorite color. The join would still have to be  
done on the client side though.

Maybe with some more time investigating this someone could come up  
with a better idea... :)

Daniel

Re: Can you connect one view to another?

Posted by Paul Davis <pa...@gmail.com>.
Matt,

In short, no. In long, also no, but it can be done server side by
persisting view results into a new db and running the subsequent
Map/Reduce on the derived database. Rinse. Repeat. Admittedly that's
not the most elegant solution and requires server side logic, but as
it turns out, the cascading is difficult to accomplish with the
current incremental view system.

HTH,
Paul Davis

On Wed, Oct 28, 2009 at 8:43 AM, Welch, Matt <Ma...@compuware.com> wrote:
> I'm pretty new to CouchDB so please pardon me if this is a silly
> question, but can you connect the results of one view to another?
>
> Imagine I have a bunch of children documents and each child has a
> favorite color. I also have a bunch of toy documents and each toy has a
> color. I want to build a list of what toys each child will like based on
> its color.
>
> I can make a view to emit(color, name) for each toy and something
> similar for each child. But then do I have to connect the two in my
> code? If your dataset gets large that seems like a lot of work gets
> pushed out to the client end. Is there any way to construct a view that
> links these together?
>
>
>
>
> The contents of this e-mail are intended for the named addressee only. It contains information that may be confidential. Unless you are the named addressee or an authorized designee, you may not copy or use it, or disclose it to anyone else. If you received it in error please notify us immediately and then destroy it.
>
>