couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gregory Tappero <cou...@gmail.com>
Subject Map reduce and weird output question
Date Sat, 06 Mar 2010 15:26:39 GMT
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 date.

For now i used this map/reduce http://friendpaste.com/5xUUQ26bbl9d5KRB8eojwe

Date.prototype.setRFC3339 = function(dString){
    var regexp =
/(\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 = dString.match(new RegExp(regexp));
        var offset = 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] != 'Z') {
            offset = (d[15] * 60) + parseInt(d[17],10);
            offset *= ((d[14] == '-') ? -1 : 1);
            this.setTime(this.getTime() - offset * 60 * 1000);
        }
    } else {
        this.setTime(Date.parse(dString));
    }
    return this;
};

var seenKeys = new Array();

function(doc) {


    if (doc.doc_type=="EdoPing" && doc.em_type==0) {
        date = new Date().setRFC3339(doc.created_at);
        var key = doc.em_uname + String(doc.created_at).substring(0,10);
        if (seenKeys[key] ==  undefined  ) {
            seenKeys[key] = 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

Mime
View raw message