2011-09-09 98 views
0

我使用Mongoid来获取Mongo数据库中某些类型的记录的计数。当运行使用JavaScript方法查询:用Ruby/Javascript查询Mongo组中的计数结果的区别

db.tags.group({ 
    cond : { tag: {$ne:'donotwant'} }, 
    key: { tag: true }, 
    reduce: function(doc, out) { out.count += 1; }, 
    initial: { count: 0 } 
}); 

我得到如下结果:

[ 
{"tag" : "thing", "count" : 4}, 
{"tag" : "something", "count" : 1}, 
{"tag" : "test", "count" : 1} 
] 

不正是我想要它做的。然而,当我使用相应Mongoid代码来执行相同的查询:

Tag.collection.group(
    :cond => {:tag => {:$ne => 'donotwant'}}, 
    :key  => [:tag], 
    :reduce => "function(doc, out) { out.count += 1 }", 
    :initial => { :count => 0 }, 
) 

参数(貌似)作为计数花车,而不是整数:

[ 
{"tag"=>"thing", "count"=>4.0}, 
{"tag"=>"something", "count"=>1.0}, 
{"tag"=>"test", "count"=>1.0} 
] 

我误解背后有什么回事场景?我需要(可以吗?)投这些计数,或者是只显示没有.0的javascript结果吗?

回答

2

JavaScript不区分浮动和整数。它有一个数字类型,实现为双精度型。因此,您在Ruby中看到的是正确的,mongo shell输出遵循javascript打印约定,并显示没有小数部分而没有'.0'的数字。'