@@ -7,16 +7,16 @@ Specify a Query
7
7
Overview
8
8
~~~~~~~~
9
9
10
- Most CRUD operations apply to a set of matched documents in a
11
- collection. By default, these operations match **all** documents in a
12
- collection. Sometimes, however, you want to match only certain items in
13
- a collection; the query document allows you to make your searches more
14
- specific. Query documents contain one or more field-level expressions
15
- that determine which documents to include in the result set. You can
16
- match fields against literal values (e.g. ``{title:'The Room'}``) or
17
- compose :manual:`query operators </reference/operator/query/>` to
18
- express more complex queries. MongoDB
19
- supports multiple types of query operator, including :
10
+ Most CRUD operations allow you to narrow the set of matched documents by
11
+ specifying matching criteria in a **query document**. Query documents contain
12
+ one or more query operators that apply to specific fields which determine which
13
+ documents to include in the result set.
14
+
15
+ In a query document, you can match fields against literal values
16
+ (e.g. ``{ title: 'The Room' }``) or you can compose
17
+ :manual:`query operators </reference/operator/query/>` to express more
18
+ complex matching criteria. In this guide, we cover the following categories
19
+ of query operators in MongoDB and show examples on how to use them :
20
20
21
21
- :manual:`Comparison Operators </reference/operator/query-comparison/>`
22
22
@@ -26,9 +26,9 @@ supports multiple types of query operator, including:
26
26
27
27
- :manual:`Evaluation Operators </reference/operator/query-evaluation/>`
28
28
29
- Consider a collection containing documents that describe an inventory of
30
- fruit. To insert this data into a collection, run the following
31
- operation :
29
+ Use the following code snippet to create a collection of documents that
30
+ describe an inventory of fruit to follow along with our query operator
31
+ examples :
32
32
33
33
.. code-block:: javascript
34
34
@@ -56,9 +56,10 @@ documents containing a field called "name" that has a value of "apples":
56
56
const cursor = collection.find(query);
57
57
await cursor.forEach(console.dir);
58
58
59
- This will print out the following results:
59
+ This code snippet returns the following results:
60
60
61
61
.. code-block:: javascript
62
+ :copyable: false
62
63
63
64
{ "_id": 1, "name": "apples", "qty": 5, "rating": 3 }
64
65
@@ -71,13 +72,13 @@ This will print out the following results:
71
72
72
73
collection.find({
73
74
rating: { $eq: 5 }
74
- }
75
+ })
75
76
76
77
.. code-block:: javascript
77
78
78
79
collection.find({
79
80
rating: 5
80
- }
81
+ })
81
82
82
83
Comparison Operators
83
84
--------------------
@@ -96,9 +97,10 @@ documents with a quantity value greater than 5 and prints them out:
96
97
const cursor = collection.find(query);
97
98
await cursor.forEach(console.dir);
98
99
99
- This will print out the following results:
100
+ This code snippet returns the following results:
100
101
101
102
.. code-block:: javascript
103
+ :copyable: false
102
104
103
105
{ "_id": 2, "name": "bananas", "qty": 7, "rating": 1 }
104
106
{ "_id": 3, "name": "oranges", "qty": 6, "rating": 2 }
@@ -115,13 +117,14 @@ that is not greater than 5 and prints them out:
115
117
116
118
.. code-block:: javascript
117
119
118
- const query = { qty: { $not { { $gt : 5 } } };
120
+ const query = { qty: { $not: { $gt: 5 }} };
119
121
const cursor = collection.find(query);
120
122
await cursor.forEach(console.dir);
121
123
122
- This will print out the following results:
124
+ This code snippet returns the following results:
123
125
124
126
.. code-block:: javascript
127
+ :copyable: false
125
128
126
129
{ "_id": 4, "name": "avocados", "qty": 3, "rating": 5 }
127
130
{ "_id": 1, "name": "apples", "qty": 5, "rating": 3 }
@@ -138,7 +141,7 @@ This will print out the following results:
138
141
collection.find({
139
142
rating: { $eq: 5 },
140
143
qty: { $gt: 4 }
141
- }
144
+ })
142
145
143
146
.. code-block:: javascript
144
147
@@ -147,7 +150,10 @@ This will print out the following results:
147
150
rating: { $eq: 5 },
148
151
qty: { $gt: 4 }
149
152
]
150
- }
153
+ })
154
+
155
+ For more information on comparison operators, see the reference manual
156
+ entry for :manual:`Comparison Query Operators </reference/operator/query-comparison/>`.
151
157
152
158
Element Operators
153
159
-----------------
@@ -163,31 +169,40 @@ field:
163
169
const cursor = collection.find(query);
164
170
await cursor.forEach(console.dir);
165
171
166
- This will print out the following results:
172
+ This code snippet returns the following results:
167
173
168
174
.. code-block:: javascript
175
+ :copyable: false
169
176
170
177
{ "_id": 2, "name": "bananas", "qty": 7, "rating": 1, "microsieverts": 0.1 }
171
178
179
+ For more information on this operator, see the reference manual entry for
180
+ the :manual:`$exists operator </reference/operator/query/exists/>`.
181
+
172
182
Evaluation Operators
173
183
--------------------
174
184
175
185
Evaluation operators allow you to execute higher level logic, like
176
186
regex and text searches, when querying for documents in a collection.
177
187
Common evaluation operators include ``$regex`` and ``$text``.
178
188
The following operation uses the evaluation operator ``$mod`` to search
179
- for documents with a quantity value that is divisible by 3:
189
+ for documents with a quantity value that is divisible by 3 with
190
+ a remainder of 0:
180
191
181
192
.. code-block:: javascript
182
193
183
194
// $mod means "modulo" and returns the remainder after division
184
- const query = { microsieverts : { $mod : 3, 0 } };
195
+ const query = { qty : { $mod: [ 3, 0 ] } };
185
196
const cursor = collection.find(query);
186
197
await cursor.forEach(console.dir);
187
198
188
- This will print out the following results:
199
+ This code snippet returns the following results:
189
200
190
201
.. code-block:: javascript
202
+ :copyable: false
191
203
192
204
{ "_id": 3, "name": "oranges", "qty": 6, "rating": 2 }
193
205
{ "_id": 4, "name": "avocados", "qty": 3, "rating": 5 }
206
+
207
+ For more information on this operator, see the reference manual entry for
208
+ the :manual:`$mod operator </reference/operator/query/mod/>`.
0 commit comments