Skip to content

Commit 5ba63b6

Browse files
committed
Aggregation example
1 parent eb96f66 commit 5ba63b6

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

examples/aggregate.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Group documents by field and calculate count.
2+
3+
coll = client[:restaurants]
4+
5+
results = coll.find.aggregate([ { '$group' => { '_id' => '$borough',
6+
'count' => { '$sum' => 1 }
7+
}
8+
}
9+
])
10+
11+
results.each do |result|
12+
puts result
13+
end
14+
15+
# Filter and group documents
16+
17+
results = coll.find.aggregate([ { '$match' => { 'borough' => 'Queens',
18+
'cuisine' => 'Brazilian' } },
19+
{ '$group' => { '_id' => '$address.zipcode',
20+
'count' => { '$sum' => 1 } } }
21+
])
22+
23+
results.each do |result|
24+
puts result
25+
end

0 commit comments

Comments
 (0)