Skip to content

Commit d7d323c

Browse files
committed
Query example
1 parent 8e1a437 commit d7d323c

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

examples/query.rb

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Query for all documents in a collection
2+
3+
cursor = client[:restaurants].find
4+
5+
cursor.each do |doc|
6+
puts doc
7+
end
8+
9+
10+
# Query for equality on a top level field
11+
12+
cursor = client[:restaurants].find('borough' => 'Manhattan')
13+
14+
cursor.each do |doc|
15+
puts doc
16+
end
17+
18+
19+
# Query by a field in an embedded document
20+
21+
cursor = client[:restaurants].find('address.zipcode' => '10075')
22+
23+
cursor.each do |doc|
24+
puts doc
25+
end
26+
27+
28+
# Query by a field in an array
29+
30+
cursor = client[:restaurants].find('grades.grade' => 'B')
31+
32+
cursor.each do |doc|
33+
puts doc
34+
end
35+
36+
37+
# Query with the greater-than operator
38+
39+
cursor = client[:restaurants].find('grades.score' => { '$gt' => 30 })
40+
41+
cursor.each do |doc|
42+
puts doc
43+
end
44+
45+
46+
# Query with the less-than operator
47+
48+
cursor = client[:restaurants].find('grades.score' => { '$lt' => 10 })
49+
50+
cursor.each do |doc|
51+
puts doc
52+
end
53+
54+
55+
# Query with a logical conjuction (AND) of query conditions
56+
57+
cursor = client[:restaurants].find({ 'cuisine' => 'Italian',
58+
'address.zipcode' => '10075'})
59+
60+
cursor.each do |doc|
61+
puts doc
62+
end
63+
64+
65+
# Query with a logical disjunction (OR) of query conditions
66+
67+
cursor = client[:restaurants].find('$or' => [{ 'cuisine' => 'Italian' },
68+
{ 'address.zipcode' => '10075'}
69+
]
70+
)
71+
72+
cursor.each do |doc|
73+
puts doc
74+
end
75+
76+
77+
# Sort query results
78+
79+
cursor = client[:restaurants].find.sort('borough' => Mongo::Index::ASCENDING,
80+
'address.zipcode' => Mongo::Index::DESCENDING)
81+
82+
cursor.each do |doc|
83+
puts doc
84+
end

0 commit comments

Comments
 (0)