Skip to content

Instantly share code, notes, and snippets.

@sadhu89
Last active January 11, 2016 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sadhu89/12fd5d519411089d5387 to your computer and use it in GitHub Desktop.
Save sadhu89/12fd5d519411089d5387 to your computer and use it in GitHub Desktop.
devices=Customer.collection.aggregate([
{
"$group" => {
_id: "$device_id",
count:{"$sum"=>1}
}
},
{
"$match"=>{
count:{"$gte"=>3}
}
},
{
"$sort"=>{
count:-1
}
}
])
report=[]
devices.each do |d|
Customer.where(device_id: d["_id"]).each do |c|
report<<"#{c.name}, #{c.email}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment