mongo db map将总和结果减少为整数 [英] mongo db map reduce sum results as integer
问题描述
我的收藏集看起来像这样
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)"
}
我猜想我的地图归约函数是正确的,但是我需要将值项求和为整数. 我还尝试了parseInt()和新的NumberLong()函数.
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.
推荐答案
以下对我有用:
在地图功能中,将发射更改为
In your map function change your emit to
emit( this.player_items[i].item, NumberInt(this.player_items[i].total_win) )};
您的减少更改总额为:
var sum = NumberInt(0);
现在对其进行测试:
> db.bet_number_play_win_stats.find({value:{$type:16}})
{ "_id" : "121511141013", "value" : 6 }
{ "_id" : "x14", "value" : 0 }
{ "_id" : "x15", "value" : 36 }
Fetched 3 record(s) in 1ms
16(十六进制10)是bson中32位整数的类型.
16 (Hex 10) is the type for 32-bit integer in bson.
如果您要汇总的项目数量不太大,建议您使用汇总框架:
If your number of items being aggregated is not too large, I would recommend looking at using aggregation framework:
> var r=db.bet_results.aggregate( [
{$unwind : "$player_items"},
{$group: { _id:"$player_items.item",
wins:{$sum:"$player_items.total_win"}
}
}
] )
> db.bet_number_play_win_agg.save(r.result)
Inserted 1 record(s) in 7ms
> > db.bet_number_play_win_agg.find({wins:{$type:16}})
{ "_id" : "121511141013", "wins" : 6 }
{ "_id" : "x15", "wins" : 36 }
{ "_id" : "x14", "wins" : 0 }
Fetched 3 record(s) in 1ms
相同的结果,保留类型,并且速度更快.
Same result, type preserved, and faster too.
这篇关于mongo db map将总和结果减少为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!