简体   繁体   中英

Map Reduce Mongodb Node JS native driver

I'm working with the Mongodb native driver using a Map reduce function. Basically I have a mediaId as a key and want to count how many medias loaded and started per mediaId. So what I've done was:

  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 mediaIds. One with 553 objects and another one with just 1 object. I've putted all in the "started" state to test this so basically the number of count 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' } } ]

It seems that one I have just one object the reduce function isn't called ( I did some tests with another collection with more than 100 mediaIds and the behavior was identical. Does anyone have an idea of what is wrong with that?

Thanks A LOT for your time, Cheers.

I sort of solved the "issue". I did the filter on the Map Function and not on the Reduce function. Something like this:

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
    });
};

Hope it helps anyone that is having the same "problem"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM