Sounds to me like you'll need to do this two queries.

First, get the user doc, so you can get the IDs of the users he
follows. Second, assuming you have a view of assets by user ID, POST
to the view with all those IDs as the keys.

(Check out the paragraph under the table of options here
http://is.gd/d7Pgu if you're unfamiliar with multi-key POSTs to
views.)

On Mon, Jun 28, 2010 at 10:53 AM, Sean Coates <[email protected]> wrote:
>> The usual way that's done is talked about here in the "One to Many"
>> section: http://wiki.apache.org/couchdb/EntityRelationship
>>
>> Basically, you map function would be something like:
>>
>>    function(doc) {
>>        if (doc.type == 'user') emit([0, doc._id], null);
>>        else if (doc.type == 'asset') emit([1, doc.ownerid], null);
>>    }
>>
>> Then if you want to get the user and the assets they own, you would
>> query the view with startkey=[0, {userid}] and endkey=[1, {userid}],
>> and if you just want the list of assets owned by a certain user, you
>> use a startkey and endkey of [1, {userid}].
>
>
> I'm sorry if I'm being thick, but I don't quite understand how this would 
> work.
> Wouldn't a startkey=[0,{userid}]&endkey=[1,{userid}] match all users if 
> {userid}="1" ?
>
> I've adjusted my test data to make it more clear what's going on (see below 
> #1). u1 is not following u2 (now).
> If I query: start_key=[0,"u1"]&end_key=[1,"u1"]
> I get user records for all users (u1, u2, u3). I don't want u2 to be 
> included. (see below #2)
>
> Perhaps I'm not doing a very good job of explaining what I need to do.
> Does this make any more sense now?
>
> S
>
> (1)
> {"total_rows":9,"offset":0,"rows":[
> {"id":"_design/test","key":"_design/test","value":{"rev":"1-fecdec6331d23f13f7f40baef4ca6564"},"doc":{"_id":"_design/test","_rev":"1-fecdec6331d23f13f7f40baef4ca6564","language":"javascript","views":{"join":{"map":"
>    function(doc) {\n       if (doc.type == 'user') emit([0, doc._id], 
> null);\n       else if (doc.type == 'asset') emit([1, doc.ownerid], null);\n  
>  }"}}}},
> {"id":"a4","key":"a4","value":{"rev":"2-e01f856361aef17a8b241114da1bcecb"},"doc":{"_id":"a4","_rev":"2-e01f856361aef17a8b241114da1bcecb","type":"asset","ownerid":"u1"}},
> {"id":"a5","key":"a5","value":{"rev":"3-14db4d330452eb9f157e4a413227f4c8"},"doc":{"_id":"a5","_rev":"3-14db4d330452eb9f157e4a413227f4c8","type":"asset","ownerid":"u1"}},
> {"id":"a6","key":"a6","value":{"rev":"3-e9f5d5c8ceaf8167c06d6751dcefb2df"},"doc":{"_id":"a6","_rev":"3-e9f5d5c8ceaf8167c06d6751dcefb2df","type":"asset","ownerid":"u2"}},
> {"id":"a7","key":"a7","value":{"rev":"3-1706f210111b6fe15dec9d09eb9af47e"},"doc":{"_id":"a7","_rev":"3-1706f210111b6fe15dec9d09eb9af47e","type":"asset","ownerid":"u3"}},
> {"id":"a8","key":"a8","value":{"rev":"3-1706f210111b6fe15dec9d09eb9af47e"},"doc":{"_id":"a8","_rev":"3-1706f210111b6fe15dec9d09eb9af47e","type":"asset","ownerid":"u3"}},
> {"id":"u1","key":"u1","value":{"rev":"6-c3b4277626cd76b76b18b5acbff2edd8"},"doc":{"_id":"u1","_rev":"6-c3b4277626cd76b76b18b5acbff2edd8","type":"user","following":["u3"]}},
> {"id":"u2","key":"u2","value":{"rev":"2-6b91b31ff9679d2a0a989f42a27ca496"},"doc":{"_id":"u2","_rev":"2-6b91b31ff9679d2a0a989f42a27ca496","type":"user","following":["u1"]}},
> {"id":"u3","key":"u3","value":{"rev":"2-6b91b31ff9679d2a0a989f42a27ca496"},"doc":{"_id":"u3","_rev":"2-6b91b31ff9679d2a0a989f42a27ca496","type":"user","following":["u1"]}}
> ]}
>
> (2)
> $ curl 
> 'http://localhost:5984/test/_design/t/_view/join?start_key=%5B0%2C%22u1%22%5D&end_key=%5B1%2C%22u1%22%5B'
> {"total_rows":8,"offset":0,"rows":[
> {"id":"u1","key":[0,"u1"],"value":null},
> {"id":"u2","key":[0,"u2"],"value":null},
> {"id":"u3","key":[0,"u3"],"value":null},
> {"id":"a4","key":[1,"u1"],"value":null},
> {"id":"a5","key":[1,"u1"],"value":null},
> {"id":"a6","key":[1,"u2"],"value":null},
> {"id":"a7","key":[1,"u3"],"value":null},
> {"id":"a8","key":[1,"u3"],"value":null}
> ]}
>
>

Reply via email to