Skip to content

Commit cb77aee

Browse files
DOCSP-13847 specify a query (#69)
* added CRUD Specify a Query page
1 parent 03ce438 commit cb77aee

File tree

15 files changed

+591
-27
lines changed

15 files changed

+591
-27
lines changed

source/fundamentals/crud.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ CRUD Operations
99

1010
/fundamentals/crud/read-operations
1111
/fundamentals/crud/write-operations
12+
/fundamentals/crud/query-document
1213
/fundamentals/crud/compound-operations
1314

1415
CRUD (Create, Read, Update, Delete) operations enable you to work with

source/fundamentals/crud/compound-operations.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,10 @@ After running this example, the output resembles the following:
305305
Additional Information
306306
----------------------
307307

308-
For more information on performing the read or write operations
309-
mentioned in this guide, see the following guides:
308+
For more information on performing the operations mentioned, see the
309+
following guides:
310310

311+
- :doc:`Specify a Query </fundamentals/crud/query-document>`
311312
- :doc:`Retrieve Data </fundamentals/crud/read-operations/retrieve>`
312313
- :doc:`Delete a Document </fundamentals/crud/write-operations/delete>`
313314
- :doc:`Update or Replace a Document </fundamentals/crud/write-operations/change-a-document>`
Lines changed: 332 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,332 @@
1+
===============
2+
Specify a Query
3+
===============
4+
5+
.. default-domain:: mongodb
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 1
11+
:class: singlecol
12+
13+
.. _query_document_golang:
14+
15+
Overview
16+
--------
17+
18+
In this guide, you can learn how to specify a query to match a subset
19+
of documents.
20+
21+
To match a subset of documents, specify a **query filter** containing
22+
your **match criteria**. Match criteria consist of the fields and
23+
values you want present in a document. A query filter contains at least
24+
one set of match criteria to determine which documents to include in the
25+
resulting set.
26+
27+
In a query filter, you can match fields with :ref:`literal values
28+
<go-literal-values>` or with :ref:`query operators
29+
<query-operators-go>`. Query operators allow you to perform mathematical
30+
or logical operations to locate documents within a collection.
31+
32+
Match criteria with literal values use the following format:
33+
34+
.. code-block:: go
35+
:copyable: false
36+
37+
filter := bson.D{{"<field>", "<value>"}}
38+
39+
Match criteria with a query operator use the following format:
40+
41+
.. code-block:: go
42+
:copyable: false
43+
44+
filter := bson.D{{"<field>", bson.D{{"<operator>", "<value>"}}}}
45+
46+
The following sections use :ref:`literal values <go-literal-values>`
47+
and :ref:`query operators <query-operators-go>` with the ``Find()``
48+
function to match a subset of documents.
49+
50+
Sample Data
51+
~~~~~~~~~~~
52+
53+
To run the examples in this guide, load the sample data into the
54+
``ratings`` collection of the ``tea`` database with the following
55+
snippet:
56+
57+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/query.go
58+
:language: go
59+
:dedent:
60+
:start-after: begin insert docs
61+
:end-before: end insert docs
62+
63+
.. include:: /includes/fundamentals/automatic-db-coll-creation.rst
64+
65+
Each document contains a rating for a type of tea and the vendors that
66+
carry them, which corresponds to the ``rating``, ``type``, and
67+
``vendor`` fields.
68+
69+
.. include:: /includes/fundamentals/truncated-id.rst
70+
71+
.. _go-literal-values:
72+
73+
Literal Values
74+
--------------
75+
76+
Literal value query filters return documents with an exact match to your
77+
match criteria.
78+
79+
.. tip::
80+
81+
If you specify an empty query filter, CRUD operations match all the
82+
documents in a collection.
83+
84+
Example
85+
~~~~~~~
86+
87+
The following example matches documents where the ``type`` is "Oolong":
88+
89+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/query.go
90+
:language: go
91+
:dedent:
92+
:start-after: begin literal
93+
:end-before: end literal
94+
95+
The expected output of this example is as follows:
96+
97+
.. code-block:: none
98+
:copyable: false
99+
100+
[{_id ObjectID("...")} {type Oolong} {rating 7} {vendor [C]}]
101+
102+
.. tip::
103+
104+
Literal value queries return the same value as the ``$eq``
105+
comparison operator. For example, the following query filters produce
106+
the same result:
107+
108+
.. code-block:: go
109+
110+
filter := bson.D{{"type", "Oolong"}}
111+
112+
.. code-block:: go
113+
114+
filter := bson.D{{"type", bson.D{{"$eq", "Oolong"}}}}
115+
116+
.. _query-operators-go:
117+
118+
Comparison
119+
----------
120+
121+
Comparison operators analyze the value in a document against the specified
122+
value in your match criteria. Common comparison operators include
123+
``$gt`` for "greater than" comparisons, ``$lte`` for "less than or equal
124+
to" comparisons, and ``$ne`` for "not equal to" comparisons.
125+
126+
Example
127+
~~~~~~~
128+
129+
The following example matches documents where the ``rating`` is less
130+
than ``7``:
131+
132+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/query.go
133+
:language: go
134+
:dedent:
135+
:start-after: begin comparison
136+
:end-before: end comparison
137+
138+
The expected output of this example is as follows:
139+
140+
.. code-block:: none
141+
:copyable: false
142+
143+
[{_id ObjectID("...")} {type English Breakfast} {rating 6}]
144+
[{_id ObjectID("...")} {type Assam} {rating 5}]
145+
146+
For a full list of comparison operators, see the :manual:`Comparison
147+
Query Operators </reference/operator/query-comparison/>` page.
148+
149+
Logical
150+
-------
151+
152+
Logical operators require at least two match criteria. They check if
153+
documents meet all, at lease one, or none of the specified criteria.
154+
Common logical operators include ``$and`` where all match criteria must
155+
be true, and ``$or`` where at least one of the match criteria must be
156+
true.
157+
158+
Example
159+
~~~~~~~
160+
161+
The following example matches documents where the ``rating`` is greater
162+
than ``7`` and less than or equal to ``10``:
163+
164+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/query.go
165+
:language: go
166+
:dedent:
167+
:start-after: begin logical
168+
:end-before: end logical
169+
170+
The expected output of this example is as follows:
171+
172+
.. code-block:: none
173+
:copyable: false
174+
175+
[{_id ObjectID("...")} {type Masala} {rating 10} {vendor [A C]}]
176+
[{_id ObjectID("...")} {type Earl Grey} {rating 8} {vendor [A B]}]
177+
178+
For a full list of logical operators, see the :manual:`Logical
179+
Query Operators </reference/operator/query-logical/>` page.
180+
181+
.. tip::
182+
183+
Multiple match criteria resembling an ``$eq`` comparison operator in
184+
a literal query return the same value as the ``$and`` logical
185+
operator. For example, the following query filters produce the same result:
186+
187+
.. code-block:: go
188+
189+
filter := bson.D{{"type", "Oolong"}, {"rating", 7}}
190+
191+
.. code-block:: go
192+
193+
filter := bson.D{
194+
{"$and",
195+
bson.A{
196+
bson.D{{"type", "Oolong"}},
197+
bson.D{{"rating", 7}},
198+
}},
199+
}
200+
201+
Element
202+
-------
203+
204+
Element operators check for the presence or type of the specified field.
205+
206+
Example
207+
~~~~~~~
208+
209+
The following example matches documents where the ``vendor`` field does
210+
not exist:
211+
212+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/query.go
213+
:language: go
214+
:dedent:
215+
:start-after: begin element
216+
:end-before: end element
217+
218+
The expected output of this example is as follows:
219+
220+
.. code-block:: none
221+
:copyable: false
222+
223+
[{_id ObjectID("...")} {type English Breakfast} {rating 6}]
224+
[{_id ObjectID("...")} {type Assam} {rating 5}]
225+
226+
For a full list of element operators, see the :manual:`Element
227+
Query Operators </reference/operator/query-element/>` page.
228+
229+
Evaluation
230+
----------
231+
232+
Evaluation operators analyze values in a document based on the
233+
specified value in your match criteria. Common evaluation operators
234+
include ``$regex`` where a field's value must match the specified
235+
regular expression and ``$text`` where the field's value must contain
236+
the specified string.
237+
238+
Example
239+
~~~~~~~
240+
241+
The following example matches documents where the ``type`` begins with
242+
the letter "E":
243+
244+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/query.go
245+
:language: go
246+
:dedent:
247+
:start-after: begin evaluation
248+
:end-before: end evaluation
249+
250+
The expected output of this example is as follows:
251+
252+
.. code-block:: none
253+
:copyable: false
254+
255+
[{_id ObjectID("...")} {type English Breakfast} {rating 6}]
256+
[{_id ObjectID("...")} {type Earl Grey} {rating 8} {vendor [A B]}]
257+
258+
For a full list of evaluation operators, see the :manual:`Evaluation
259+
Query Operators </reference/operator/query-evaluation/>` page.
260+
261+
Array
262+
-----
263+
264+
Array operators check the values or amount of elements in an array field.
265+
266+
Example
267+
~~~~~~~
268+
269+
The following example matches documents where the ``vendor`` contains "C":
270+
271+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/query.go
272+
:language: go
273+
:dedent:
274+
:start-after: begin array
275+
:end-before: end array
276+
277+
The expected output of this example is as follows:
278+
279+
.. code-block:: none
280+
:copyable: false
281+
282+
[{_id ObjectID("...")} {type Masala} {rating 10} {vendor [A C]}]
283+
[{_id ObjectID("...")} {type Oolong} {rating 7} {vendor [C]}]
284+
285+
For a full list of array operators, see the :manual:`Array
286+
Query Operators </reference/operator/query-array/>` page.
287+
288+
Bitwise
289+
-------
290+
291+
Bitwise operators convert a numeric field from a base-10 (decimal)
292+
number into the corresponding base-2 (binary) number. They check whether
293+
the value in a document has the same bits set as the value in your match
294+
criteria.
295+
296+
Example
297+
~~~~~~~
298+
299+
The following example matches documents where the ``rating`` has the same
300+
bits set as ``6`` (which is "00000110"):
301+
302+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/query.go
303+
:language: go
304+
:dedent:
305+
:start-after: begin bitwise
306+
:end-before: end bitwise
307+
308+
The expected output of this example is as follows:
309+
310+
.. code-block:: none
311+
:copyable: false
312+
313+
[{_id ObjectID("...")} {type English Breakfast} {rating 6}]
314+
[{_id ObjectID("...")} {type Oolong} {rating 7} {vendor [C]}]
315+
316+
For a full list of bitwise operators, see the :manual:`Bitwise
317+
Query Operators </reference/operator/query-bitwise/>` page.
318+
319+
Additional Information
320+
----------------------
321+
322+
.. For information on specifying a geospatial query, see the guide on
323+
.. :doc:`Geospatial Data </fundamentals/crud/read-operations/geospatial>`.
324+
325+
API Documentation
326+
~~~~~~~~~~~~~~~~~
327+
328+
For more information on any of the functions or types used in this
329+
guide, see the following API Documentation:
330+
331+
- `Find() <{+api+}/mongo#Collection.Find>`__
332+
- `Cursor <{+api+}/mongo#Cursor>`__

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,10 @@ After running this example, the output resembles the following:
143143
Additional Information
144144
----------------------
145145

146-
For more information on the read operations used, see the following guides:
146+
For more information about the operations mentioned, see the following
147+
guides:
147148

149+
- :doc:`Specify a Query </fundamentals/crud/query-document>`
148150
- :doc:`Retrieve Data </fundamentals/crud/read-operations/retrieve>`
149151
- :doc:`Sort Results </fundamentals/crud/read-operations/sort>`
150152
- :doc:`Skip Returned Results </fundamentals/crud/read-operations/skip>`

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,12 @@ After running this example, the output resembles the following:
160160
Additional Information
161161
----------------------
162162

163-
For more information on performing read operations, see our guide on
164-
:doc:`retrieving data </fundamentals/crud/read-operations/retrieve>`.
163+
For more information about the operations mentioned, see the following
164+
guides:
165+
166+
- :doc:`Specify a Query </fundamentals/crud/query-document>`
167+
- :doc:`Retrieve Data </fundamentals/crud/read-operations/retrieve>`
168+
- :doc:`Compound Operations </fundamentals/crud/compound-operations>`
165169

166170
.. For more information on aggregation, see the
167171
.. :doc:`Aggregation </fundamentals/aggregation>` guide.

0 commit comments

Comments
 (0)