map = %Q{
function() {
emit(this.userinfo.id, {totalPrice: this.paycost, profit: this.profit, count: 1})
}
}
mapStatistic = %Q{
function() {
emit(this.userinfo_id, {totalPrice: this.total_price, profit: this.profit, count: 1})
}
}
reduce = %Q{
function(key, values) {
profit = 0.0;
income = 0.0;
count = 0;
incomes = []
profits = []
values.forEach(function(value) {
income += value.totalPrice;
profit += value.profit;
count += value.count;
});
return {profit: profit, income: income, count: count}
}
}
statistics = OrderStatistic.where({:complete_date => {"$gte" => startDate, "$lte" => endDate}, :userinfo_id => @current_user.userinfo.id}).map_reduce(mapStatistic, reduce).out(:inline => true)
Rails.logger.info "0000000statistic=======#{statistics.inspect}==sCount=#{sCount}"
日志打印出来是 1111111statistic======={"_id"=>"564a90a903d0b85d0a000000", "value"=>{"profit"=>38110.0, "income"=>NaN, "count"=>499.0}} income => NaN 如何 total_price 小数点保留 2 位的相加后有问题,profit 也是 float,小数点后面没有有效位没有问题。