Map Reduce Mongodb Node Native JS Driver

I work with the native Mongodb driver using the map reduction feature. Basically, I have mediaId as a key and you want to calculate how much media is loaded and running on mediaId. So what I did:

  var map = function(){
        emit(this.media.id, {
            count: 1,
            played: 0,
            ph: this.project.id,
            title: this.media.title,
            media: this.media.id,
            origin: this.origin,
            thumbnail: this.media.thumbnail,
            mediaDuration: this.media.mediaDuration,
            state: this.state
        });
    };

    var reduce = function(k, vals) {
        result = {
            count: 0,
            played: 0,
            ph: '',
            title: '',
            media: '',
            origin: '',
            thumbnail: '',
            mediaDuration: 0,
            state: ''
        };

        vals.forEach(function(doc){
            result.count += doc.count;
            result.ph = doc.ph;
            result.title = doc.title;
            result.media = doc.media;
            result.thumbnail = doc.thumbnail;
            result.mediaDuration = doc.mediaDuration;
            result.state = doc.state;
            result.origin = doc.origin;
            if(doc.state === "started") {
                result.played += 1;
            }
        });
        return result;
    };

In my test collection, I have 2 different media. One with 553 objects, and the other with one object. I put everything in the β€œinitial” state to check it this way, basically the number of samples should be equal to the number of played. When I run the Map / Reduce function, it returns to me (I used the "toArray" function of the mongodb native driver):

[ { _id: '12398asdsa9802193810asd120',
value: 
 { count: 1,
   played: 0,
   ph: '123213ased12231',
   title: 'xxxxxxxxxxxxxxxxxxxxxxxxxxx',
   media: '1xxxxxxxxxxxxxxxxxxxxxxxxxxx1',
   origin: 'http://www.google.com',
   thumbnail: 'http://cache.ohinternet.com/images/0/0e/Forever_Alone.png',
   mediaDuration: 12321321,
   state: 'started' } },
{ _id: '2c9f94b42f5b5114012f5b92ea430066',
value: 
 { count: 553,
   played: 155,
   ph: '316',
   title: 'xxxxxxxxxxxxxxxxxxxxxxxxxxx',
   media: '2xxxxxxxxxxxxxxxxxxxxxxxxxxx2',
   origin: 'http://localhost:9000/views/index.html',
   thumbnail: null,
   mediaDuration: null,
   state: 'started' } } ]

, , ( 100 mediaIds, . - , ?

, .

+3
1

"". , . - :

var map = function(){
    if(this.media.state==="started") {
       var played = 1;
}else{var played = 0;}
    emit(this.media.id, {
        count: 1,
        played: played,
        ph: this.project.id,
        title: this.media.title,
        media: this.media.id,
        origin: this.origin,
        thumbnail: this.media.thumbnail,
        mediaDuration: this.media.mediaDuration,
        state: this.state
    });
};

, , ""

+2

All Articles