在MongoDB group()函数中使用Long的累加器而不是Double [英] Long accumulator instead of Double in MongoDB group() function
问题描述
group()
函数累加这些值时,JavaScript解释器将所有内容转换为双倍,最终结果为Double。 这是我的组命令:
{
/ pre>
...
初始值:{count:0},
reduce:function(o,a){a.count + = o.count;}
}
有没有办法告诉翻译,
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 likecount: 0L
orcount: 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屋!