5つを超えるレポートを取得したユーザーを見つけるには、mongodb集計フレームワークを使用する必要があります
モンゴシェルで
db.reports.aggregate(
// Group by user_id
{$group:{_id:'$user_id',count:{$sum:1},
// Add the fields to result set along with grouped result
name:{$addToSet:'$any_field_you_need_to_return'}}},
// And return only the users who got greater than 5 reports
{$match:{count:{$gt:5}}})
アップデート
Aggregate
でのみ導入されましたmoped v1.3
。最新の原付をインストールして実行するには、gemfileに変更を加える必要がありますbundle install
gem 'moped', :git=>'https://github.com/mongoid/moped.git', :branch => "master"
そして、あなたは次のような骨材を使うことができます
Reports.collection.aggregate(
{'$group' => {'_id' => '$user_id','count' => {'$sum' => 1},
'name' => {'$addToSet' => '$any_field_you_need_to_return'}}},
{'$match' => {'count' => {'$gt' => 5 }}})