Return-Path: X-Original-To: apmail-couchdb-user-archive@www.apache.org Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 386EBDF1C for ; Tue, 26 Jun 2012 13:12:44 +0000 (UTC) Received: (qmail 24156 invoked by uid 500); 26 Jun 2012 13:12:42 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 24115 invoked by uid 500); 26 Jun 2012 13:12:42 -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 24104 invoked by uid 99); 26 Jun 2012 13:12:42 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 26 Jun 2012 13:12:42 +0000 X-ASF-Spam-Status: No, hits=1.5 required=5.0 tests=FSL_RCVD_USER,HTML_MESSAGE,RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of rizalp@gmail.com designates 209.85.214.180 as permitted sender) Received: from [209.85.214.180] (HELO mail-ob0-f180.google.com) (209.85.214.180) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 26 Jun 2012 13:12:37 +0000 Received: by obbuo19 with SMTP id uo19so8907610obb.11 for ; Tue, 26 Jun 2012 06:12:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=HewZVhZ0K0g1F8WEYo70DzEmgOp3JT/zw2jL+A3JeuU=; b=AhQeeMdGjkefOvUqtWDbJEEXkjHbY1kwpEpH952h/ZgzE+8584GMs1Wun+h5Fv0FP4 dfuHXv6y+iizI8dViZPOsKQRcHOkaUUD7RvsHJS2QYnBItBWC83zem1Xmp/d4QJDjknq WTyAsAu7Vv/O7H7Ng9d+/CpFxQ23GZrcdqwWdAYNNqsacYnqig6KccFfDSb9ihi0XyV8 mErOG5OJngZnozzE+0IyzNVvEzpW1DcYwVJYQPneMIw0LIV7FJACXDEq7ajCFFiZosjo o7AEJPVHMgHHnBoFWswEX8E9m6xjZFTG+hgJ8RhG3NFIqsZQQj6Z06vawDHmDqKIkghi v5sA== MIME-Version: 1.0 Received: by 10.182.117.71 with SMTP id kc7mr16063538obb.62.1340716337083; Tue, 26 Jun 2012 06:12:17 -0700 (PDT) Received: by 10.76.2.142 with HTTP; Tue, 26 Jun 2012 06:12:17 -0700 (PDT) In-Reply-To: References: Date: Tue, 26 Jun 2012 20:12:17 +0700 Message-ID: Subject: Re: How to merge objects attributes from reduce to rereduce function. From: Mohammad Prabowo To: user@couchdb.apache.org Content-Type: multipart/alternative; boundary=f46d04446a4d04336704c35fd90c X-Virus-Checked: Checked by ClamAV on apache.org --f46d04446a4d04336704c35fd90c Content-Type: text/plain; charset=ISO-8859-1 I also asked this question in stackoverflow: http://stackoverflow.com/q/11207961/1448852 On Tue, Jun 26, 2012 at 7:52 PM, Mohammad Prabowo wrote: > This is my JSON schema: > > { > "emp_no": .., > "salary": .., > "from_date": .., > "to_date": .., > "type" : "salaries" > } > > { > "emp_no": .., > "title": .., > "from_date": .., > "to_date" : .., > "type" : "titles" > } > > What i wanted to do, is to find the average salary for each active title. > Active titles are document with "from_date" attribute set to "9999-01-01" > > Here is my Map Function > > function(doc) { > if (doc.type == 'salaries') { > var dateSalaries = null; > dateSalaries = doc.to_date.split("-"); > if(dateSalaries[0].localeCompare("9999") == 0){ > emit(doc.emp_no, ["salary", doc.salary] ); > } > } else if (doc.type == 'titles') { > var dateTitles = null; > dateTitles = doc.to_date.split("-"); > if(dateTitles[0].localeCompare("9999") == 0){ > emit(doc.emp_no, ["title", doc.title]); > } > } > } > > Here is the resulting key value pairs emited: > > http://i.imgur.com/o1Qxz.png > > Now, i want to reduce it into single key-value pair, with the value > outputted is set into javascript object like this > > { > "engineer" : 64342, > "senior engineer" : 123111, > "staff" : ..., > "senior staf" : ..., > . > . > . > } > > Here's how i planned to do it: First, in reduce step, i'm gonna return > object that merge properties from the same emp_no. Then, in reduce step, > i'm gonna create a new object that has properties name based on reduced > value before. > > It's hard to explain, so here is my reduce function: > > function(keys, values, rereduce) { > var i, l, attr, sal, rv = {}; > if (rereduce) { > for (i = 0, l = values.length; i if (values[i].hasOwnProperty('salary')) { > attr = values[i].title; > sal = values[i].salary; > if (rv[attr] instanceof Array) { > rv[attr].push(sal); > } else{ > rv[attr] = []; > rv[attr].push(sal); > } > } > } > for (var x in rv) { > if (rv.hasOwnProperty(x)) { > var totalSalary = 0; > for (i = 0, l = values.length; i totalSalary += rv[x][i]; > } > rv[x] = totalSalary / rv[x].length; > } > } > } else { > for (i = 0, l = values.length; i switch (values[i][0]) { > case "title" : rv["title"] = values[i][1]; break; > case "salary": rv["salary"] = values[i][1]; break; > } > } > } > return rv; > } > > The resulting value here is reduced value, which is what i expected: > http://i.imgur.com/SnlOU.png > > But, when i set the grouping value to 'none' in futon, it's not what i > wanted: > http://i.imgur.com/nfDn6.png > > Could someone help me to solves this? > > --f46d04446a4d04336704c35fd90c--