Return-Path: Delivered-To: apmail-couchdb-commits-archive@www.apache.org Received: (qmail 23992 invoked from network); 13 Apr 2009 22:06:48 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 13 Apr 2009 22:06:48 -0000 Received: (qmail 96288 invoked by uid 500); 13 Apr 2009 22:06:48 -0000 Delivered-To: apmail-couchdb-commits-archive@couchdb.apache.org Received: (qmail 96210 invoked by uid 500); 13 Apr 2009 22:06:48 -0000 Mailing-List: contact commits-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@couchdb.apache.org Delivered-To: mailing list commits@couchdb.apache.org Received: (qmail 96201 invoked by uid 500); 13 Apr 2009 22:06:48 -0000 Delivered-To: apmail-incubator-couchdb-commits@incubator.apache.org Received: (qmail 96198 invoked by uid 99); 13 Apr 2009 22:06:48 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 13 Apr 2009 22:06:48 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.130] (HELO eos.apache.org) (140.211.11.130) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 13 Apr 2009 22:06:47 +0000 Received: from eos.apache.org (localhost [127.0.0.1]) by eos.apache.org (Postfix) with ESMTP id 3458A118BB for ; Mon, 13 Apr 2009 22:06:27 +0000 (GMT) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Apache Wiki To: couchdb-commits@incubator.apache.org Date: Mon, 13 Apr 2009 22:06:26 -0000 Message-ID: <20090413220627.26358.28464@eos.apache.org> Subject: [Couchdb Wiki] Update of "EntityRelationship" by WoutMertens X-Virus-Checked: Checked by ClamAV on apache.org Dear Wiki user, You have subscribed to a wiki page or wiki category on "Couchdb Wiki" for change notification. The following page has been changed by WoutMertens: http://wiki.apache.org/couchdb/EntityRelationship The comment on the change is: better views ------------------------------------------------------------------------------ You would use this method when there is potentially a large number of contacts in a group '''and''' a large number of groups. In that case embedding a list of keys could result in huge documents so we have to resort to writing out the list in many documents. - If you then want to know who is in a group you'll need to use the view + If you then want to know who is in a group you'll need to use the view (fetch descending to get the group info first) {{{ "map":function(doc) { if (doc.type == 'relationship') { emit(doc.group_id,doc.contact_id); + } else if (doc.type == 'group') { + emit(doc._id,doc); } } }}} - To know what groups a contact belongs to you need to use + To know what groups a contact belongs to you can use {{{ "map":function(doc) { if (doc.type == 'relationship') { - emit(doc.contact_id,doc.group_id); + emit([doc.contact_id,1],doc.group_id); + } else if (doc.type == 'contact') { + emit([doc._id,0],doc); } } }}} + Note that this view uses key arrays to enforce sorting, just to show you the possible variations. The disadvantage is that you can't use ''key="Scott"'' to search for Scotts groups, you need to use ''startkey=["Scott"]&endkey=["Scott",{}]''. - As you can see, to retrieve further information about the group or the contact, you'll need to send another query to CouchDB. + Unlike the previous method, you can't use ''include_docs=true'' now to get all information about the contacts that are in a group or the groups that a contact has. The reason is that the original documents that were used in generating the view are not the contact or group documents, they are the relationship documents. If you want that information, you'll have to fetch it separately. + If this is becoming a problem due to roundtrip times to the database, an acceptable solution is to duplicate the needed information in the relationship documents. You trade the inconvenience of maintaining multiple copies of the same data for the low access time to that data. Unless you have extreme requirements however, you do not need to do this. +