On Apr 30, 2008, at 15:43, Guby wrote: >>> >> Thanks Jan, >> >> That certainly makes it easier. I think I understand your warning >> regarding doubling of the data. Essentially a results of a view are >> pre-calculated when the view is save? So using the id's to then >> retrieve >> the related object would be a lot more efficient in terms of storage. >> The trade off being multiple requests to get the actual objects? > Just as a side note: > The views are actually calculated or populated on the first request > of the view (or when documents are saved if you activate that (http://wiki.apache.org/couchdb/RegeneratingViewsOnUpdate) > ) and not when you save the view document itself. Just a side note to the side note: There is no way to 'activate' on-write view creation :) Cheers Jan -- > > >> Would it be possible to retrieve all the documents in one request. If >> the id's where continuous it would be easy to use start_key and >> end_key >> but what if they are dis-continuous? > It is actually not necessary that the children's ID's are sequential! > In Jan's example above you actually get all the children when you > request the view, and therefore only have to make one request to the > database to get all the relations! > The actual ID's of the children doesn't matter to much as they are > using the parents ID as the key! > > Using the following view: > >>> function(doc) { >>> for(var idx in doc.parents) { >>> map(doc.parents[idx], doc); >>> } >>> } > > You could get all the children of the parent with ID "foo" like this: > > GET /db_name/_view/relations/children_of?key=foo > > As long as the design document has the ID "_design/relations" and > the view the name "children_of" > > If you happen to be programming in ruby you can see how I > implemented this in CouchObject. The changes aren't in the current > gem yet, but I'll push out a new gem soon. CouchObject now supports > has_many, has_one and belongs_to relations using the view technique > above. > You can get the code from the git repository at http://gitorious.org/projects/couchobject > > Best regards > Sebastian >