Return-Path: Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: (qmail 64344 invoked from network); 3 May 2009 00:57:32 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 3 May 2009 00:57:32 -0000 Received: (qmail 75815 invoked by uid 500); 3 May 2009 00:57:31 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 75719 invoked by uid 500); 3 May 2009 00:57:30 -0000 Mailing-List: contact user-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@couchdb.apache.org Delivered-To: mailing list user@couchdb.apache.org Received: (qmail 75709 invoked by uid 99); 3 May 2009 00:57:30 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 03 May 2009 00:57:30 +0000 X-ASF-Spam-Status: No, hits=3.7 required=10.0 tests=HTML_MESSAGE,SPF_PASS,WEIRD_PORT X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of nborwankar@gmail.com designates 74.125.46.28 as permitted sender) Received: from [74.125.46.28] (HELO yw-out-2324.google.com) (74.125.46.28) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 03 May 2009 00:57:24 +0000 Received: by yw-out-2324.google.com with SMTP id 2so1639350ywt.5 for ; Sat, 02 May 2009 17:57:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:sender:received:in-reply-to :references:date:x-google-sender-auth:message-id:subject:from:to :content-type; bh=Z8MfWZKLlqU+J3aDxMXBwa5/pNjgF5c3gsVqwAC8nI8=; b=D6ttWoqHJ8x9DLiTI9aa2MEWpc+dvqPLBUxU7Y3knwOBNm0LSCCnmitm3Lb5aXhsfv CkmwNQb5LfG8EjCANtB+Tfeyz2S9+VSv/Sjv2I/edXgxPk/WWofUtm2d89+FWgYTI49S QezUHWSQ+i8ijCbGLqPuYWbPzR7OIUnXZexUs= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:content-type; b=P1GAau8kIGTRIL07ff3VLCYa0go2332HzIPgLzRzNs3UkLXR1KM94a0N6Lxx3npacc XSEOa8NAfcvPKfGfyh4vW7qyhK1f4yFiqH4dCW+PZdWmZHF9smAIY+3fR/kBirWv3sNo 1Ihok7TNH3TzP8WRbAj1SYJ87jAVAEEE/QrWg= MIME-Version: 1.0 Sender: nborwankar@gmail.com Received: by 10.231.20.3 with SMTP id d3mr492277ibb.48.1241312223360; Sat, 02 May 2009 17:57:03 -0700 (PDT) In-Reply-To: <41139fcb0905012207x123ada68gee1d8cf8775288d8@mail.gmail.com> References: <921000904291341w4ebb8f90yf54c1ae6d3696fcf@mail.gmail.com> <41139fcb0905012207x123ada68gee1d8cf8775288d8@mail.gmail.com> Date: Sat, 2 May 2009 17:57:03 -0700 X-Google-Sender-Auth: 64a93332375ee271 Message-ID: <921000905021757w644ba5a5x9e6d7fda013ba28c@mail.gmail.com> Subject: Re: how to access key names of a document? From: Nitin Borwankar To: user@couchdb.apache.org Content-Type: multipart/alternative; boundary=000325574a4e20f91f0468f7869a X-Virus-Checked: Checked by ClamAV on apache.org --000325574a4e20f91f0468f7869a Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Anand, Thanks much. I did muddle around and figure that out in the meanwhile by playing around in futon. But when I create the design doc and put this code as { [...] views: { cnt : { map: function () {....}, reduce: function() {...} } } 37% of all statistics are made up on the spot ------------------------------------------------------------------------------------- Nitin Borwankar nborwankar@gmail.com On Fri, May 1, 2009 at 10:07 PM, Anand Chitipothu wrote: > > I have a database with documents that have varying number of fields. > > I need to create a view that displays > > > > { field_name1 : count, ..... } > > > > i.e. a report that displays how many times a field occurs in the db. > > It's like a word frequency vector in Lucene except for the fact that > these > > are field names. > > > > So I am missing something basic but I need to loop over all keys of a doc > > without knowing their names in advance. > > so I do something like > > > > var x = {} > > for ( var k in doc.keys ) { > > x[k] = 1 > > } > > emit ( x, 1 ) <---- is this the right thing to emit ? > > No, you should do this: > > function(doc) { > for (var k in doc) > emit(k, 1); > } > > And your reduce function should be: > > function(keys, values, rereduce) { > return sum(values); > } > > I'm assuming that you want global frequency of k1, k2 etc in documents > like {"k1": "v1", "k2", "v2", ..} > > If name of your design document is d and name of your view is v, you > can get the frequency from: > > http://localhost:5984/dbname/_design/d/_view/v?reduce=true > > > newbie questions > > a) whats the syntax to access all keys of a doc > > You can achieve this by writing a view with the following map function: > > function(doc) { > var keys = []; > for (key in doc) > keys.push(key); > emit(doc.id, keys); > } > > > b) what's the right thing to emit > > c) what do I do in the reduce phase ? > > Answered above. > > -Anand > --000325574a4e20f91f0468f7869a--