在MongoDB group()函数中使用Long的累加器而不是Double [英] Long accumulator instead of Double in MongoDB group() function

查看:206
本文介绍了在MongoDB group()函数中使用Long的累加器而不是Double的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过官方Java API使用MongoDB。我可以存储和恢复Long值,而不需要额外的努力。但是,当我尝试使用 group()函数累加这些值时,JavaScript解释器将所有内容转换为双倍,最终结果为Double。



这是我的组命令:

  {
...
初始值:{count:0},
reduce:function(o,a){a.count + = o.count;}
}
/ pre>

有没有办法告诉翻译, count 其实是一个长?一些类似 count:0L count:Long(0)?或者我应该在Java方面积累吗?

解决方案

这是因为组命令实际上运行map / reduce,并且map / reduce是一个javascript。在javascript默认的数字类型是一个double,因为这样它返回双打。



所以你可以用 NumberLong(.. )如果你想看到长命令结果:

  {
...
initial:{count:new NumberLong(0)},
reduce:function(o,a){a.count + = new NumberLong(o.count);}
}

没有测试这个,但几乎肯定它应该可以工作。


I am using MongoDB via official Java API. I can store and retrive Long values without any extra effort. But when I try to accumulate these values using group() function, JavaScript interpreter converts everything into Doubles and the final result ends up being a Double.

Here is my group command:

{
    ...
    initial: { count: 0 },
    reduce: "function (o, a) { a.count += o.count; }"
}

Is there a way to tell the interpreter that count is in fact a Long? Something like count: 0L or count: Long(0)? Or should I do the accumulation on Java side?

解决方案

This is because group command actually run map/reduce, and map/reduce is a javascript. In the javascript default number type is a double, because of this it return doubles.

So you can probably wrap your numbers with NumberLong(..) if you wanna see long in group command result:

{
    ...
    initial: { count: new NumberLong(0) },
    reduce: "function (o, a) { a.count += new NumberLong(o.count); }"
}

Not tested this, but almost sure that it should work.

这篇关于在MongoDB group()函数中使用Long的累加器而不是Double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆