You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Travis Jensen <tr...@gmail.com> on 2011/09/06 20:05:51 UTC

Show functions accessing two (or more) documents

I have a question about "joining" documents as part of a show function. For
illustration's sake, let's say I have POS data stored in CouchDB (this isn't
my actual use case, but it is an easily understood use case, so let's go
with it. In other words, don't tell me what I'm doing wrong with invoices,
tell me what I'm doing wrong with Couch :). Among other things, the POS has
the following:

{ "_id": "invoice1001",
  "customer": "customer125",
  "items": [ { "itemnum": "item1", "qty": 8, "unitprice": 1.25 },
             { "itemnum": "item125", "qty": 1, "unitprice": 79.99 } ]
}

Customers are stored in the database as well:

{ "_id": "customer125",
  "address": "123 N Main St"
  "city": "Smallville"
  "state": "KY"
  "zip": "20000"
}

And items exist in the database:

{ "_id": "item125",
  "name": "Foobazzer 1.0",
  "desc": "A widget for foobazzing"
}

Now I'd like to build a printable invoice. This seems like a perfect job for
a show function, but I don't know how to do the "join" part where I look up
the customer and items based on their respective IDs.  Is this problem
solvable on the server?

Thanks.

tj
-- 
*Travis Jensen*
***
*Read the Software Maven @ http://softwaremaven.innerbrane.com/
Read my LinkedIn profile @ http://www.linkedin.com/in/travisjensen
Read my Twitter mumblings @ http://twitter.com/SoftwareMaven
Send me email @ travis.jensen@gmail.com

**What kind of guy calls himself the Software Maven???**

Re: Show functions accessing two (or more) documents

Posted by Robert Newson <rn...@apache.org>.
A show function only operates on a single document, so I assume you
really mean a list function.

You should be able, in your list function, to call getRow() twice, and
then you combine the documents and send() the result.

Obviously you'll need to arrange your view so that these related items
appear together in the results, see the View Collation wiki page on
that.

B.

On 6 September 2011 19:05, Travis Jensen <tr...@gmail.com> wrote:
> I have a question about "joining" documents as part of a show function. For
> illustration's sake, let's say I have POS data stored in CouchDB (this isn't
> my actual use case, but it is an easily understood use case, so let's go
> with it. In other words, don't tell me what I'm doing wrong with invoices,
> tell me what I'm doing wrong with Couch :). Among other things, the POS has
> the following:
>
> { "_id": "invoice1001",
>  "customer": "customer125",
>  "items": [ { "itemnum": "item1", "qty": 8, "unitprice": 1.25 },
>             { "itemnum": "item125", "qty": 1, "unitprice": 79.99 } ]
> }
>
> Customers are stored in the database as well:
>
> { "_id": "customer125",
>  "address": "123 N Main St"
>  "city": "Smallville"
>  "state": "KY"
>  "zip": "20000"
> }
>
> And items exist in the database:
>
> { "_id": "item125",
>  "name": "Foobazzer 1.0",
>  "desc": "A widget for foobazzing"
> }
>
> Now I'd like to build a printable invoice. This seems like a perfect job for
> a show function, but I don't know how to do the "join" part where I look up
> the customer and items based on their respective IDs.  Is this problem
> solvable on the server?
>
> Thanks.
>
> tj
> --
> *Travis Jensen*
> ***
> *Read the Software Maven @ http://softwaremaven.innerbrane.com/
> Read my LinkedIn profile @ http://www.linkedin.com/in/travisjensen
> Read my Twitter mumblings @ http://twitter.com/SoftwareMaven
> Send me email @ travis.jensen@gmail.com
>
> **What kind of guy calls himself the Software Maven???**
>