Skip to content

Commit fa6f85f

Browse files
authored
DOCSP-31495: specify query technical review (#46)
1 parent c79d10e commit fa6f85f

File tree

2 files changed

+51
-91
lines changed
  • source
    • fundamentals/crud/read-operations
    • includes/fundamentals/code-snippets/crud

2 files changed

+51
-91
lines changed

source/fundamentals/crud/read-operations/query.txt

Lines changed: 34 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,31 @@ of documents.
2121
To match a subset of documents, specify a **query filter** containing
2222
your **match criteria**. Match criteria consist of the fields and
2323
values you want documents to match. A query filter contains at least
24-
one set of match criteria to determine which documents to return. An
25-
empty query filter matches all of the documents in a collection.
24+
one set of match criteria to determine which documents to return. If you
25+
use an empty query filter in a find operation, the driver matches all
26+
the documents in the collection.
2627

2728
In a query filter, you can match fields with :ref:`literal values
2829
<rust-literal-values>` or with query operators. Query operators allow
2930
you to perform mathematical or logical operations to locate documents within a collection.
3031

31-
To match criteria with literal values use the following format:
32+
To match documents by using literal values, use the following format:
3233

3334
.. code-block:: rust
3435
:copyable: false
3536

3637
let filter = doc! { "<field>": "<value>" };
3738

38-
To match criteria that include query operators use the following format:
39+
To create match criteria that include query operators, use the following
40+
format:
3941

4042
.. code-block:: rust
4143
:copyable: false
4244

4345
let filter = doc! { "<field>": doc! { "<operator>": "<value>" } };
4446

45-
The examples in the following sections show how to specify queries by using the ``find()``
46-
method to match documents in a collection.
47+
The examples in the following sections show how to specify queries by
48+
using the ``find()`` method to match documents in a collection.
4749

4850
This guide includes the following sections:
4951

@@ -71,7 +73,7 @@ This guide includes the following sections:
7173
query based on the equivalent bits set of a base-10 value
7274

7375
- :ref:`Array <rust-array-operators>`: describes how to query
74-
a collection based on data within an array-valued field.
76+
a collection based on data within an array-valued field
7577

7678
.. _rust-query-sample-data:
7779

@@ -115,20 +117,14 @@ that has the value of ``pear``:
115117
:language: json
116118
:visible: false
117119

118-
{
119-
"_id": 4,
120-
"name": "pear",
121-
"quantity": 28,
122-
"vendors": [
123-
"A",
124-
"C"
125-
]
126-
}
120+
Document({"_id": Int32(4), "name": String("pear"), "quantity":
121+
Int32(28), "vendors": Array([String("A"), String("C")])})
127122

128123
.. note::
129124

130-
Literal value queries function identically to queries that use the ``$eq`` comparison
131-
operator. For example, the following queries are equivalent:
125+
Literal value queries function identically to queries that use the
126+
``$eq`` comparison operator. For example, the following queries are
127+
equivalent:
132128

133129
.. code-block:: rust
134130

@@ -171,25 +167,12 @@ documents with a ``quantity`` value greater than ``5``:
171167
:language: json
172168
:visible: false
173169

174-
{
175-
"_id": 1,
176-
"name": "orange",
177-
"quantity": 7
178-
}
179-
{
180-
"_id": 3,
181-
"name": "banana",
182-
"quantity": 36
183-
}
184-
{
185-
"_id": 4,
186-
"name": "pear",
187-
"quantity": 28,
188-
"vendors": [
189-
"A",
190-
"C"
191-
]
192-
}
170+
Document({"_id": Int32(1), "name": String("orange"), "quantity":
171+
Int32(7)})
172+
Document({"_id": Int32(3), "name": String("banana"), "quantity":
173+
Int32(36)})
174+
Document({"_id": Int32(4), "name": String("pear"), "quantity":
175+
Int32(28), "vendors": Array([String("A"), String("C")])})
193176

194177
For more information on comparison operators, see :manual:`Comparison
195178
Query Operators </reference/operator/query-comparison/>` in the Server manual.
@@ -224,11 +207,8 @@ divisible by ``3``:
224207
:language: json
225208
:visible: false
226209

227-
{
228-
"_id": 3,
229-
"name": "banana",
230-
"quantity": 36
231-
}
210+
Document({"_id": Int32(3), "name": String("banana"), "quantity":
211+
Int32(36)})
232212

233213
.. note::
234214

@@ -247,7 +227,7 @@ divisible by ``3``:
247227
.. code-block:: rust
248228

249229
my_coll.find(doc! {
250-
$and: vec! [
230+
$and: [
251231
doc! { "price": { "$eq": 5 }},
252232
doc! { "quantity": { "$gt": 4 }}
253233
]
@@ -284,12 +264,8 @@ field:
284264
:language: json
285265
:visible: false
286266

287-
{
288-
"_id": 2,
289-
"name": "apple",
290-
"quantity": 4,
291-
"description": "Granny Smith"
292-
}
267+
Document({"_id": Int32(2), "name": String("apple"), "quantity":
268+
Int32(4), "description": String("Granny Smith")})
293269

294270
For a full list of element operators, see :manual:`Element
295271
Query Operators </reference/operator/query-element/>` in the Server manual.
@@ -324,11 +300,8 @@ for documents with a ``quantity`` value that is divisible by 3:
324300
:language: json
325301
:visible: false
326302

327-
{
328-
"_id": 3,
329-
"name": "banana",
330-
"quantity": 36
331-
}
303+
Document({"_id": Int32(3), "name": String("banana"), "quantity":
304+
Int32(36)})
332305

333306
For a full list of evaluation operators, see :manual:`Evaluation
334307
Query Operators </reference/operator/query-evaluation/>` in the Server manual.
@@ -362,11 +335,8 @@ bits set as ``7``, which is equivalent to ``00000111`` in binary:
362335
:language: json
363336
:visible: false
364337

365-
{
366-
"_id": 1,
367-
"name": "orange",
368-
"quantity": 7
369-
}
338+
Document({"_id": Int32(1), "name": String("orange"), "quantity":
339+
Int32(7)})
370340

371341
For a full list of bitwise operators, see :manual:`Bitwise
372342
Query Operators </reference/operator/query-bitwise/>` in the Server manual.
@@ -399,22 +369,18 @@ The following example matches documents where the ``vendor`` contains
399369
:language: json
400370
:visible: false
401371

402-
{
403-
"_id": 4,
404-
"name": "pear",
405-
"quantity": 28,
406-
"vendors": [
407-
"A",
408-
"C"
409-
]
410-
}
372+
Document({"_id": Int32(4), "name": String("pear"), "quantity":
373+
Int32(28), "vendors": Array([String("A"), String("C")])})
411374

412375
For a full list of bitwise operators, see :manual:`Array
413376
Query Operators </reference/operator/query-array/>` in the Server manual.
414377

415378
Additional Information
416379
----------------------
417380

381+
To learn more about find operations, see the :ref:`rust-retrieve-guide`
382+
guide.
383+
418384
To learn more about query operators, see :manual:`Query Selectors
419385
</reference/operator/query/>` in the Server manual.
420386

source/includes/fundamentals/code-snippets/crud/query.rs

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
use std::env;
12
use bson::Document;
23
use futures::TryStreamExt;
3-
use mongodb::{ bson::doc, Client, Collection, options::{ FindOptions } };
4+
use mongodb::{ bson::doc, Client, Collection };
45

56
#[tokio::main]
67
async fn main() -> mongodb::error::Result<()> {
@@ -22,70 +23,63 @@ async fn main() -> mongodb::error::Result<()> {
2223
//begin-literal
2324
let query = doc! { "name": "pear" };
2425
let mut cursor = my_coll.find(query, None).await?;
25-
while let Some(result) = cursor.try_next().await? {
26-
let doc = bson::from_document(result)?;
27-
println!("{}", serde_json::to_string_pretty(&doc).unwrap());
26+
while let Some(doc) = cursor.try_next().await? {
27+
println!("{:?}", doc);
2828
}
2929
//end-literal
3030
println!("");
3131
//begin-comparison
3232
// $gt means "greater than"
3333
let query = doc! { "quantity": doc! { "$gt": 5 } };
3434
let mut cursor = my_coll.find(query, None).await?;
35-
while let Some(result) = cursor.try_next().await? {
36-
let doc = bson::from_document(result)?;
37-
println!("{}", serde_json::to_string_pretty(&doc).unwrap());
35+
while let Some(doc) = cursor.try_next().await? {
36+
println!("{:?}", doc);
3837
}
3938
//end-comparison
4039
println!("");
4140
//begin-logical
4241
let query =
43-
doc! { "$and": vec! [
42+
doc! { "$and": [
4443
doc! { "quantity": doc! { "$gt": 10 } },
4544
doc! { "quantity": doc! { "$mod": [ 3, 0 ] } }
4645
]
4746
};
4847
let mut cursor = my_coll.find(query, None).await?;
49-
while let Some(result) = cursor.try_next().await? {
50-
let doc = bson::from_document(result)?;
51-
println!("{}", serde_json::to_string_pretty(&doc).unwrap());
48+
while let Some(doc) = cursor.try_next().await? {
49+
println!("{:?}", doc);
5250
}
5351
//end-logical
5452
println!("");
5553
//begin-element
5654
let query = doc! { "description": doc! { "$exists": true } };
5755
let mut cursor = my_coll.find(query, None).await?;
58-
while let Some(result) = cursor.try_next().await? {
59-
let doc = bson::from_document(result)?;
60-
println!("{}", serde_json::to_string_pretty(&doc).unwrap());
56+
while let Some(doc) = cursor.try_next().await? {
57+
println!("{:?}", doc);
6158
}
6259
//end-element
6360
println!("");
6461
//begin-evaluation
6562
// $mod means "modulo" and checks if the remainder is a specific value
6663
let query = doc! { "quantity": doc! { "$mod": [ 3, 0 ] } };
6764
let mut cursor = my_coll.find(query, None).await?;
68-
while let Some(result) = cursor.try_next().await? {
69-
let doc = bson::from_document(result)?;
70-
println!("{}", serde_json::to_string_pretty(&doc).unwrap());
65+
while let Some(doc) = cursor.try_next().await? {
66+
println!("{:?}", doc);
7167
}
7268
//end-evaluation
7369
println!("");
7470
//begin-bitwise
7571
let query = doc! { "quantity": doc! { "$bitsAllSet": 7 } };
7672
let mut cursor = my_coll.find(query, None).await?;
77-
while let Some(result) = cursor.try_next().await? {
78-
let doc = bson::from_document(result)?;
79-
println!("{}", serde_json::to_string_pretty(&doc).unwrap());
73+
while let Some(doc) = cursor.try_next().await? {
74+
println!("{:?}", doc);
8075
}
8176
//end-bitwise
8277
println!("");
8378
//begin-array
8479
let query = doc! { "vendors": doc! { "$elemMatch": { "$eq": "C" } } };
8580
let mut cursor = my_coll.find(query, None).await?;
86-
while let Some(result) = cursor.try_next().await? {
87-
let doc = bson::from_document(result)?;
88-
println!("{}", serde_json::to_string_pretty(&doc).unwrap());
81+
while let Some(doc) = cursor.try_next().await? {
82+
println!("{:?}", doc);
8983
}
9084
//end-array
9185

0 commit comments

Comments
 (0)