You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by 7zark7 <7z...@gmail.com> on 2010/09/15 23:10:34 UTC

One-sided 1-to-many relationship

Given doc type A and B:

A has a 1-to-many relationship with B (each A knows of many Bs)
Each B can be known by many As, but does not know which.
A has an array of its known B keys.

A doc:
{
   "_id": "...",
   "kind": "A",
   "b_IDs": ["...", "...", "...", "..."]
}


B doc:
{
   "_id": "...",
   "kind": "B"
}


If I know the key for A, how can I retrieve all related Bs in one view 
or list call?
I'd like to avoid middle-ware that makes sequential calls.

Thanks!

Re: One-sided 1-to-many relationship

Posted by 7zark7 <7z...@gmail.com>.
Perfect!  Thanks!



On 9/15/10 2:18 PM, Kenneth Tyler wrote:
> I think this is the feature you want to use.
>
> from the wiki:
> Linked documents
>
> *This is a new feature in couchdb 0.11*
>
> If you emit an object value which has *{'_id': XXX}* then include_docs will
> fetch the document with id XXX rather than the document which was processed
> to emit the key/value pair.
>
> This means that if one document contains the ids of other documents, it can
> cause those documents to be fetched in the view too, adjacent to the same
> key if required.
>
> For example, if you have the following hierarchically-linked documents:
>
> [
> { "_id": "11111" },
> { "_id": "22222", "ancestors": ["11111"], "value": "hello" },
> { "_id": "33333", "ancestors": ["22222","11111"], "value": "world" }
> ]
>
> you can emit the values with the ancestor documents adjacent to them in the
> view like this:
>
> function(doc) {
>    if (doc.value) {
>      emit([doc.value, 0], null);
>      if (doc.ancestors) {
>        for (var i in doc.ancestors) {
>          emit([doc.value, Number(i)+1], {_id: doc.ancestors[i]});
>        }
>      }
>    }
>
>
> }
>
>
> more at http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views
>
>
> ken tyler
>
> On Wed, Sep 15, 2010 at 2:10 PM, 7zark7<7z...@gmail.com>  wrote:
>
>> Given doc type A and B:
>>
>> A has a 1-to-many relationship with B (each A knows of many Bs)
>> Each B can be known by many As, but does not know which.
>> A has an array of its known B keys.
>>
>> A doc:
>> {
>>   "_id": "...",
>>   "kind": "A",
>>   "b_IDs": ["...", "...", "...", "..."]
>> }
>>
>>
>> B doc:
>> {
>>   "_id": "...",
>>   "kind": "B"
>> }
>>
>>
>> If I know the key for A, how can I retrieve all related Bs in one view or
>> list call?
>> I'd like to avoid middle-ware that makes sequential calls.
>>
>> Thanks!
>>
>


Re: One-sided 1-to-many relationship

Posted by Kenneth Tyler <ke...@8thfold.com>.
I think this is the feature you want to use.

from the wiki:
Linked documents

*This is a new feature in couchdb 0.11*

If you emit an object value which has *{'_id': XXX}* then include_docs will
fetch the document with id XXX rather than the document which was processed
to emit the key/value pair.

This means that if one document contains the ids of other documents, it can
cause those documents to be fetched in the view too, adjacent to the same
key if required.

For example, if you have the following hierarchically-linked documents:

[
{ "_id": "11111" },
{ "_id": "22222", "ancestors": ["11111"], "value": "hello" },
{ "_id": "33333", "ancestors": ["22222","11111"], "value": "world" }
]

you can emit the values with the ancestor documents adjacent to them in the
view like this:

function(doc) {
  if (doc.value) {
    emit([doc.value, 0], null);
    if (doc.ancestors) {
      for (var i in doc.ancestors) {
        emit([doc.value, Number(i)+1], {_id: doc.ancestors[i]});
      }
    }
  }


}


more at http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views


ken tyler

On Wed, Sep 15, 2010 at 2:10 PM, 7zark7 <7z...@gmail.com> wrote:

> Given doc type A and B:
>
> A has a 1-to-many relationship with B (each A knows of many Bs)
> Each B can be known by many As, but does not know which.
> A has an array of its known B keys.
>
> A doc:
> {
>  "_id": "...",
>  "kind": "A",
>  "b_IDs": ["...", "...", "...", "..."]
> }
>
>
> B doc:
> {
>  "_id": "...",
>  "kind": "B"
> }
>
>
> If I know the key for A, how can I retrieve all related Bs in one view or
> list call?
> I'd like to avoid middle-ware that makes sequential calls.
>
> Thanks!
>

Re: One-sided 1-to-many relationship

Posted by Robert Newson <ro...@gmail.com>.
Does this help?

http://wiki.apache.org/couchdb/View_collation

B.

On Wed, Sep 15, 2010 at 10:10 PM, 7zark7 <7z...@gmail.com> wrote:
> Given doc type A and B:
>
> A has a 1-to-many relationship with B (each A knows of many Bs)
> Each B can be known by many As, but does not know which.
> A has an array of its known B keys.
>
> A doc:
> {
>  "_id": "...",
>  "kind": "A",
>  "b_IDs": ["...", "...", "...", "..."]
> }
>
>
> B doc:
> {
>  "_id": "...",
>  "kind": "B"
> }
>
>
> If I know the key for A, how can I retrieve all related Bs in one view or
> list call?
> I'd like to avoid middle-ware that makes sequential calls.
>
> Thanks!
>