My collections seems like this
{
"bet_session_id": ObjectId("51a60cba6ef215d019000299"),
"date": ISODate("2013-05-29T14:13:14.572Z"),
"player_items": [
{
"chip": NumberInt(1),
"item": "x14",
"ratio": NumberInt(35),
"total_win": NumberInt(0)
},
{
"chip": NumberInt(1),
"item": "x15",
"ratio": NumberInt(35),
"total_win": NumberInt(36)
},
{
"chip": NumberInt(1),
"item": "121511141013",
"ratio": NumberInt(5),
"total_win": NumberInt(6)
},
],
"user_id": "7876010",
}
,
{
"bet_session_id": ObjectId("51a60cba6ef215d019000299"),
"date": ISODate("2013-05-29T14:13:14.572Z"),
"player_items": [
{
"chip": NumberInt(1),
"item": "x14",
"ratio": NumberInt(35),
"total_win": NumberInt(0)
},
{
"chip": NumberInt(1),
"item": "x15",
"ratio": NumberInt(35),
"total_win": NumberInt(36)
},
{
"chip": NumberInt(1),
"item": "121511141013",
"ratio": NumberInt(5),
"total_win": NumberInt(6)
},
{
"chip": NumberInt(1),
"item": "12151114",
"ratio": NumberInt(8),
"total_win": NumberInt(9)
},
{
"chip": NumberInt(1),
"item": "1514",
"ratio": NumberInt(17),
"total_win": NumberInt(18)
},
{
"chip": NumberInt(1),
"item": "1215",
"ratio": NumberInt(17),
"total_win": NumberInt(18)
},
{
"chip": NumberInt(1),
"item": "151814171316",
"ratio": NumberInt(5),
"total_win": NumberInt(6)
},
{
"chip": NumberInt(1),
"item": "151413",
"ratio": NumberInt(11),
"total_win": NumberInt(12)
},
{
"chip": NumberInt(1),
"item": "15181417",
"ratio": NumberInt(8),
"total_win": NumberInt(9)
}
],
"user_id": "9034906623",
}
using above the data
i need to calculate player total wins
and My map reduce functions is here :
var mapBetPlayWinStats = function() {
for (i=0; i<this.player_items.length; i++ ) emit( this.player_items[i].item, this.player_items[i].total_win )};
var reduceBetPlayWinStats = "function(item,values) { var sum = 0; sum += values; return sum; }"
db.bet_results.mapReduce(mapBetPlayWinStats, reduceBetPlayWinStats, {out: "bet_number_play_win_stats"});
RESULTS:
{
"_id": "red",
"value": "20,50,20,NumberLong(20),NumberLong(20),100"
}
{
"_id": "odd",
"value": "NumberLong(0),NumberLong(0)"
}
{
"_id": "even",
"value": "0,20,NumberLong(20),NumberLong(20)"
}
i guess my map reduce functions is correct but i need to sum value items as integer.
i also tried to parseInt() and new NumberLong() functions.
See Question&Answers more detail:
os