couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Matthew Woodward <>
Subject Re: how i can do a join in couchdb ?
Date Wed, 22 Feb 2012 17:28:29 GMT
Without addressing your exact situation, the gist is you have your view
output they key in such a way that things are grouped/joined appropriately.

Simple example, say you want to output a parent document and its child
documents, then the next parent and its child documents.

Assume parent document has an ID, and child document has the parent ID in
it as the field "parentID". Also assume that each document has a type
field, and in this example we'll use types of "parent" and "child."

In your view you'd do something like:

if (doc.type == 'parent') {
    emit([doc._id, 0], ... whatever value you want here ...);
} else if (doc.type == 'child') {
    emit([doc.parentID, 1], ... whatever value you want here ...);

That will render the parent followed by its children because the view will
be collated on parent ID and 0 first, then parent ID and 1 next, due to the
array key of a matching ID and then the 0 and 1 integers. Since the view is
sorted based on that array key everything in the output is grouped properly.

Matthew Woodward / Twitter: @mpwoodward

Please do not send me proprietary file formats such as Word, PowerPoint,
etc. as attachments.

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message