File tree 3 files changed +3
-8
lines changed
3 files changed +3
-8
lines changed Original file line number Diff line number Diff line change 1
1
# Insert a document
2
+
2
3
require 'date'
3
4
4
5
result = client [ :restaurants ] . insert_one ( {
Original file line number Diff line number Diff line change 1
1
# Create a single field index
2
+
2
3
result = client [ :restaurants ] . indexes . create ( cuisine : Mongo ::Index ::ASCENDING )
3
4
4
5
# Create a compound index
6
+
5
7
result = client [ :restaurants ] . indexes . create ( cuisine : 1 , zipcode : Mongo ::Index ::DESCENDING )
Original file line number Diff line number Diff line change 6
6
puts doc
7
7
end
8
8
9
-
10
9
# Query for equality on a top level field
11
10
12
11
cursor = client [ :restaurants ] . find ( 'borough' => 'Manhattan' )
15
14
puts doc
16
15
end
17
16
18
-
19
17
# Query by a field in an embedded document
20
18
21
19
cursor = client [ :restaurants ] . find ( 'address.zipcode' => '10075' )
24
22
puts doc
25
23
end
26
24
27
-
28
25
# Query by a field in an array
29
26
30
27
cursor = client [ :restaurants ] . find ( 'grades.grade' => 'B' )
33
30
puts doc
34
31
end
35
32
36
-
37
33
# Query with the greater-than operator
38
34
39
35
cursor = client [ :restaurants ] . find ( 'grades.score' => { '$gt' => 30 } )
42
38
puts doc
43
39
end
44
40
45
-
46
41
# Query with the less-than operator
47
42
48
43
cursor = client [ :restaurants ] . find ( 'grades.score' => { '$lt' => 10 } )
51
46
puts doc
52
47
end
53
48
54
-
55
49
# Query with a logical conjuction (AND) of query conditions
56
50
57
51
cursor = client [ :restaurants ] . find ( { 'cuisine' => 'Italian' ,
61
55
puts doc
62
56
end
63
57
64
-
65
58
# Query with a logical disjunction (OR) of query conditions
66
59
67
60
cursor = client [ :restaurants ] . find ( '$or' => [ { 'cuisine' => 'Italian' } ,
73
66
puts doc
74
67
end
75
68
76
-
77
69
# Sort query results
78
70
79
71
cursor = client [ :restaurants ] . find . sort ( 'borough' => Mongo ::Index ::ASCENDING ,
You can’t perform that action at this time.
0 commit comments