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结果吗?