Return-Path: Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: (qmail 76039 invoked from network); 7 Mar 2010 21:03:20 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 7 Mar 2010 21:03:20 -0000 Received: (qmail 90318 invoked by uid 500); 7 Mar 2010 21:02:58 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 90286 invoked by uid 500); 7 Mar 2010 21:02:58 -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 90277 invoked by uid 99); 7 Mar 2010 21:02:58 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 07 Mar 2010 21:02:58 +0000 X-ASF-Spam-Status: No, hits=2.2 required=10.0 tests=FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,SPF_PASS,T_TO_NO_BRKTS_FREEMAIL,WEIRD_PORT X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of pawelstawicki@gmail.com designates 74.125.78.27 as permitted sender) Received: from [74.125.78.27] (HELO ey-out-2122.google.com) (74.125.78.27) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 07 Mar 2010 21:02:55 +0000 Received: by ey-out-2122.google.com with SMTP id 4so756823eyf.41 for ; Sun, 07 Mar 2010 13:02:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :from:date:message-id:subject:to:content-type; bh=/Fn6msxXDrEAIjUhlyYq+I+S20oesMA0k6H+fL3pvYA=; b=GBQTsAJHO7gj1Ml+NjA/uQX1ncc4l/30gHJ111It/IQ4oeAOC2warbRFRCCr7kSAge g82Pm8l7bZX1oyExP6bGaT/yCd7HnyuEINUCb3+JjAHUf8g+OT0nIlxE9KBzLIfAYWR9 6V+MQGVZnK2OZV8C5diEUHwfrKAODXIMbWiLA= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; b=IEF0g6dG0MG8/o4uODFhEjNJSF9Ij5iVOF0KbuwkVhTF9iwKKIhBMIvs9uOhWaPote XivwWn0eaY3x2XkvXvZbm9GORRJxAtFq1ZDJkvix4gc8il6qMaANrtu4zqQwIL/bvfYW N+V1zZdj0CJRaJHAu7Mq9ThTX5PQvnflnc7TE= MIME-Version: 1.0 Received: by 10.213.100.161 with SMTP id y33mr2655684ebn.27.1267995754115; Sun, 07 Mar 2010 13:02:34 -0800 (PST) In-Reply-To: References: From: =?UTF-8?Q?Pawe=C5=82_Stawicki?= Date: Sun, 7 Mar 2010 22:02:14 +0100 Message-ID: <6adfa88d1003071302x1a7e95b7k1ac303abd4a7a5f2@mail.gmail.com> Subject: Re: Map reduce and weird output question To: user@couchdb.apache.org Content-Type: multipart/alternative; boundary=001636c5a5fc8031b304813c44a9 --001636c5a5fc8031b304813c44a9 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Just emit all documents with em_type =3D 0 in map function, with [date, em_uname] as key. Then count in reduce. Map: function(doc) { if (doc.em_type =3D 0) { //If you only want to count, you can emit anything (e.g. 1) instead of doc here. emit([date, em_uname], doc); } } Reduce: function(keys, values, rereduce) { if (!rereduce) { return count_of_values; } else { return sum_of_values; } //If you return 1 from emit instead of doc, then count_of_values =3D=3D sum_of_values } Then you can handle everything by grouping: http://yourserver:5984/yourdb/_view/yourview?group_level=3D2 or group=3Dtrue Regards -- Pawe=C5=82 Stawicki http://pawelstawicki.blogspot.com http://szczecin.jug.pl On Sat, Mar 6, 2010 at 16:26, Gregory Tappero wrote: > Hello everyone, > > I have the following EdoPing 's type of documents > > { > "_id": "22add509c1e7bc286832edc5bfe99ce5", > "_rev": "1-49663ab8778f445e481143120d0d7086", > "doc_type": "EdoPing", > "em_uname": "student1", > "em_gid": 1, > "created_at": "2010-03-03T14:18:19Z", > "em_ip": "92.154.70.148", > "em_type": 0, > "room_url": "z2fudcvcrfa3reaydatre", > "room_users": [ > "tutorsbox" > ] > } > > i would like to count all unique em_uname of em_type 0 on a given day dat= e. > > For now i used this map/reduce > http://friendpaste.com/5xUUQ26bbl9d5KRB8eojwe > > Date.prototype.setRFC3339 =3D function(dString){ > var regexp =3D > > /(\d\d\d\d)(-)?(\d\d)(-)?(\d\d)(T)?(\d\d)(:)?(\d\d)(:)?(\d\d)(\.\d+)?(Z|(= [+-])(\d\d)(:)?(\d\d))/; > > if (dString.toString().match(new RegExp(regexp))) { > var d =3D dString.match(new RegExp(regexp)); > var offset =3D 0; > > this.setUTCDate(1); > this.setUTCFullYear(parseInt(d[1],10)); > this.setUTCMonth(parseInt(d[3],10) - 1); > this.setUTCDate(parseInt(d[5],10)); > this.setUTCHours(parseInt(d[7],10)); > this.setUTCMinutes(parseInt(d[9],10)); > this.setUTCSeconds(parseInt(d[11],10)); > if (d[12]) > this.setUTCMilliseconds(parseFloat(d[12]) * 1000); > else > this.setUTCMilliseconds(0); > if (d[13] !=3D 'Z') { > offset =3D (d[15] * 60) + parseInt(d[17],10); > offset *=3D ((d[14] =3D=3D '-') ? -1 : 1); > this.setTime(this.getTime() - offset * 60 * 1000); > } > } else { > this.setTime(Date.parse(dString)); > } > return this; > }; > > var seenKeys =3D new Array(); > > function(doc) { > > > if (doc.doc_type=3D=3D"EdoPing" && doc.em_type=3D=3D0) { > date =3D new Date().setRFC3339(doc.created_at); > var key =3D doc.em_uname + String(doc.created_at).substring(0,10); > if (seenKeys[key] =3D=3D undefined ) { > seenKeys[key] =3D 1; > emit([date.getFullYear(), parseInt(date.getMonth())+1, > date.getDate() ] , 1); > } > } > } > > > It works when saved for this first time but as soon as new EdoPings > get added it starts emitting rows it has already seen ! (same key) > creating faulty count results. > > Is it ok to have seenKeys outside of the doc function() ? > What other way could i use to get the same results ? > > Thanks, > > Greg > --001636c5a5fc8031b304813c44a9--