Rails Rails 中用 Mongoid 的 map_reduce 中 float 类型相加问题,

zhaopei · 2015年12月03日 · 最后由 zhaopei 回复于 2015年12月14日 · 2073 次阅读
 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,小数点后面没有有效位没有问题。

没有人出现 这个问题吗?

其中某个文档缺字段?

#2 楼 @aptx4869 没有缺少字段,都有的,好像是值溢出了吧。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号