Skip to content

Commit 8db7563

Browse files
author
Chris Cho
authored
DOCSP-13961: query operators fixes (#133)
* DOCSP-13961: query operators fixes
1 parent 43cc163 commit 8db7563

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed

source/fundamentals/crud/query-document.txt

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ Specify a Query
77
Overview
88
~~~~~~~~
99

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:
2020

2121
- :manual:`Comparison Operators </reference/operator/query-comparison/>`
2222

@@ -26,9 +26,9 @@ supports multiple types of query operator, including:
2626

2727
- :manual:`Evaluation Operators </reference/operator/query-evaluation/>`
2828

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:
3232

3333
.. code-block:: javascript
3434

@@ -56,9 +56,10 @@ documents containing a field called "name" that has a value of "apples":
5656
const cursor = collection.find(query);
5757
await cursor.forEach(console.dir);
5858

59-
This will print out the following results:
59+
This code snippet returns the following results:
6060

6161
.. code-block:: javascript
62+
:copyable: false
6263

6364
{ "_id": 1, "name": "apples", "qty": 5, "rating": 3 }
6465

@@ -71,13 +72,13 @@ This will print out the following results:
7172

7273
collection.find({
7374
rating: { $eq: 5 }
74-
}
75+
})
7576

7677
.. code-block:: javascript
7778

7879
collection.find({
7980
rating: 5
80-
}
81+
})
8182

8283
Comparison Operators
8384
--------------------
@@ -96,9 +97,10 @@ documents with a quantity value greater than 5 and prints them out:
9697
const cursor = collection.find(query);
9798
await cursor.forEach(console.dir);
9899

99-
This will print out the following results:
100+
This code snippet returns the following results:
100101

101102
.. code-block:: javascript
103+
:copyable: false
102104

103105
{ "_id": 2, "name": "bananas", "qty": 7, "rating": 1 }
104106
{ "_id": 3, "name": "oranges", "qty": 6, "rating": 2 }
@@ -115,13 +117,14 @@ that is not greater than 5 and prints them out:
115117

116118
.. code-block:: javascript
117119

118-
const query = { qty: { $not { { $gt : 5 } } };
120+
const query = { qty: { $not: { $gt: 5 }}};
119121
const cursor = collection.find(query);
120122
await cursor.forEach(console.dir);
121123

122-
This will print out the following results:
124+
This code snippet returns the following results:
123125

124126
.. code-block:: javascript
127+
:copyable: false
125128

126129
{ "_id": 4, "name": "avocados", "qty": 3, "rating": 5 }
127130
{ "_id": 1, "name": "apples", "qty": 5, "rating": 3 }
@@ -138,7 +141,7 @@ This will print out the following results:
138141
collection.find({
139142
rating: { $eq: 5 },
140143
qty: { $gt: 4 }
141-
}
144+
})
142145

143146
.. code-block:: javascript
144147

@@ -147,7 +150,10 @@ This will print out the following results:
147150
rating: { $eq: 5 },
148151
qty: { $gt: 4 }
149152
]
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/>`.
151157

152158
Element Operators
153159
-----------------
@@ -163,31 +169,40 @@ field:
163169
const cursor = collection.find(query);
164170
await cursor.forEach(console.dir);
165171

166-
This will print out the following results:
172+
This code snippet returns the following results:
167173

168174
.. code-block:: javascript
175+
:copyable: false
169176

170177
{ "_id": 2, "name": "bananas", "qty": 7, "rating": 1, "microsieverts": 0.1 }
171178

179+
For more information on this operator, see the reference manual entry for
180+
the :manual:`$exists operator </reference/operator/query/exists/>`.
181+
172182
Evaluation Operators
173183
--------------------
174184

175185
Evaluation operators allow you to execute higher level logic, like
176186
regex and text searches, when querying for documents in a collection.
177187
Common evaluation operators include ``$regex`` and ``$text``.
178188
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:
180191

181192
.. code-block:: javascript
182193

183194
// $mod means "modulo" and returns the remainder after division
184-
const query = { microsieverts: { $mod : 3, 0 } };
195+
const query = { qty: { $mod: [ 3, 0 ] } };
185196
const cursor = collection.find(query);
186197
await cursor.forEach(console.dir);
187198

188-
This will print out the following results:
199+
This code snippet returns the following results:
189200

190201
.. code-block:: javascript
202+
:copyable: false
191203

192204
{ "_id": 3, "name": "oranges", "qty": 6, "rating": 2 }
193205
{ "_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

Comments
 (0)