mutual friend using MapReduce in MongoDB

对着背影说爱祢 提交于 2019-12-25 14:12:16

问题


i am trying MapReduce in MongoDB, i have MongoDB collection with following data type

{ "_id" : ObjectId("57aea85af405910cfcd2bfeb"), "friendList" : [ "Karma", " Tom", " Ram", " Bindu", " Shiva", " Kishna", " Bikash", " Bakshi", " Dinesh" ], "user" : "Hari" }

{ "_id" : ObjectId("57aea85bf405910cfcd2bfec"), "friendList" : [ "Karma", " Sita", " Bakshi", " Hanks", " Shyam", " Bikash" ], "user" : "Howard" }

{ "_id" : ObjectId("57aea85cf405910cfcd2bfed"), "friendList" : [ "Dinesh", " Ram", " Hanks", " Bindu", " Howard", " Bikash", " Shyam", " Bakshi" ], "user" : "Sita" }

{ "_id" : ObjectId("57aea85cf405910cfcd2bfee"), "friendList" : [ "Hanks", " Tom", " Karma", " Hari", " Dinesh" ], "user" : "Shiva" }

{ "_id" : ObjectId("57aea85cf405910cfcd2bfef"), "friendList" : [ "Bakshi", " Kishna", " Hanks", " Shiva", " Bindu", " Hari", " Karma", " Sita" ], "user" : "Dinesh" }

My Code

map = Code("""
           function () {
               for (var i=0; i<this.friendList.length; i++)
            {
            var friendValue = this.friendList[i];
            var userValue = this.user;

            var tempUserValue = userValue.toLowerCase().trim();
            var tempFriendValue = friendValue.toLowerCase().trim();

                if(tempUserValue>tempFriendValue)
                    {
                    var temp = userValue;
                    userValue = friendValue;
                    friendValue = temp; 
                    }

            var key = {"user" : userValue, "friend" : friendValue};


            emit(key, {"friendList":this.friendList});              
            }

                   }""") 

reduce = Code("""
            function(key, values){
            var combinedfriendList = {"friendList":[]};

            for (var i in values){
                var inter = values[i];
                    for(var j in inter.friendList){
                        combinedfriendList.friendList.push(inter.friendList[j]);
                        }
                        }


            return {"_id": {"user":key.user, "friend": key.friend}, "value":combinedfriendList};

            }""")

Here in map function, i have done sorting and also composite key is formed so that same key passes through same reducers i.e input to reduce() function

Map() output:

{"_id" : {"user" : "Dinesh","friend" : " Hari"},"value" : {"friendList" : ["Bakshi"," Kishna"," Hanks"," Shiva"," Bindu"," Hari"," Karma"," Sita"    ]}}
{"_id" : {"user" : "Dinesh","friend" : " Hari"},"value" : {"friendList" : ["Karma"," Tom"," Ram"," Bindu"," Shiva"," Kishna"," Bikash"," Bakshi"," Dinesh"]}}

And now in reducer i want to combined friendList having same key.But here my reduce function is not working?

Expected Output

{"_id" : {"user" : "Dinesh","friend" : " Hari"},"value" : {"friendList" : ["Bakshi"," Kishna"," Hanks"," Shiva"," Bindu"," Hari"," Karma"," Sita",
"Karma"," Tom"," Ram"," Bindu"," Shiva"," Kishna"," Bikash"," Bakshi"," Dinesh"]}}

来源:https://stackoverflow.com/questions/39003704/mutual-friend-using-mapreduce-in-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!