让我们说
class User
include Mongoid::Document
field :sign_in_count, :type => Integer, :default => 0
field :some_other_count, :type => Integer, :default => 0
end
我需要得到一个包含两个字段总和的哈希所以结果将如下所示:
{ "sign_in_count" => 4, "some_other_count" => 12 }
如果我做
sign_in_sum = User.sum("sign_in_count")
some_other_sum = User.sum("some_other_count")
我看到两个 mongo 查询,但我需要一次完成。请帮忙。