Skip to content

Commit c658d71

Browse files
rustagirgithub-actions[bot]
authored andcommitted
DOCSP-43464: operations with builders (#206)
* DOCSP-43464: operations with builders * fix desc * shared include fix? * shared include fix? * revert * netlify trigger * small fix * LM PR fixes 1 * LM PR fixes 2 * link fix * reformat comments * JT tech review 2 * fix code example * JT tech review comments (cherry picked from commit e6182b8)
1 parent 1360e2b commit c658d71

File tree

6 files changed

+445
-2
lines changed

6 files changed

+445
-2
lines changed

source/aggregation.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,12 @@ The examples in this section are adapted from the {+mdb-server+} manual.
228228
Each example provides a link to the sample data that you can insert into
229229
your database to test the aggregation operation.
230230

231+
.. tip:: Operations with Builders
232+
233+
You can use builders to support non-aggregation operations such as
234+
find and update operations. To learn more, see the :ref:`php-builders`
235+
guide.
236+
231237
Filter and Group Example
232238
~~~~~~~~~~~~~~~~~~~~~~~~
233239

source/builders.txt

Lines changed: 326 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,326 @@
1+
.. _php-builders:
2+
3+
========================
4+
Operations with Builders
5+
========================
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
.. facet::
14+
:name: genre
15+
:values: reference
16+
17+
.. meta::
18+
:keywords: aggregation, query, code example, type-safe
19+
20+
Overview
21+
--------
22+
23+
In this guide, you can learn about the **builder classes**
24+
that the {+library-short+} provides to create types used in your
25+
operations. You can use the builder classes and factory methods from the
26+
Aggregation Builder feature to create filters for other operations such
27+
as find, update, and delete operations. To learn more about the Aggregation
28+
Builder, see the :ref:`php-aggregation-builder-api` section of the
29+
Aggregation guide.
30+
31+
Using builders to create queries helps you identify errors at compile
32+
time and avoid them at runtime. This guide provides information on
33+
builder classes that you can use to perform the following tasks:
34+
35+
- :ref:`php-builders-filter`
36+
- :ref:`php-builders-update`
37+
- :ref:`php-builders-changestream`
38+
39+
.. note:: Setting Operation Options
40+
41+
You cannot specify options by using factory methods for the equivalent
42+
aggregation stages. For example, you cannot use the ``Stage::limit()`` method
43+
to set a returned documents limit on your find operation. You must specify
44+
options by using the string-based syntax, as shown in the following code:
45+
46+
.. code-block:: php
47+
48+
$options = [
49+
'limit' => 5,
50+
'<option name>' => '<specification>',
51+
];
52+
53+
This guide provides examples of how to use builders in non-aggregation
54+
operations. To view aggregation examples, see the :ref:`php-aggregation`
55+
guide.
56+
57+
Sample Data
58+
~~~~~~~~~~~
59+
60+
The examples in this guide use the ``shipwrecks`` collection in the
61+
``sample_geospatial`` database from the :atlas:`Atlas sample datasets
62+
</sample-data>`. To access this collection from your PHP application,
63+
instantiate a ``MongoDB\Client`` that connects to an Atlas cluster
64+
and assign the following value to your ``$collection`` variable:
65+
66+
.. literalinclude:: /includes/builders.php
67+
:language: php
68+
:dedent:
69+
:start-after: start-db-coll
70+
:end-before: end-db-coll
71+
72+
To learn how to create a free MongoDB Atlas cluster and load the sample
73+
datasets, see the :atlas:`Get Started with Atlas </getting-started>` guide.
74+
75+
Import Builder Classes
76+
----------------------
77+
78+
To run the examples in this guide, you must import the following classes
79+
into your application:
80+
81+
.. literalinclude:: /includes/builders.php
82+
:language: php
83+
:dedent:
84+
:start-after: start-imports
85+
:end-before: end-imports
86+
87+
.. _php-builders-filter:
88+
89+
Create a Filter
90+
---------------
91+
92+
You can use factory methods from the ``Query`` builder class to create
93+
filter definitions to use in find, update, and delete operations. When
94+
using the ``Query::query()`` factory method to create queries, you can
95+
use named argument syntax and implement type safety. To learn more about
96+
creating filters, see the :ref:`php-specify-query` guide.
97+
98+
The following steps describe how to create a filter definition by using
99+
builders:
100+
101+
1. Call the ``Query::query()`` method to create a query.
102+
103+
#. Pass the field name to filter on and a factory method from the
104+
``Query`` class. You can pass one or more pairs of field names and
105+
criteria in the filter to apply multiple clauses.
106+
107+
The following code shows the template to create a filter definition by
108+
using builders:
109+
110+
.. code-block:: php
111+
112+
$filter = Query::query(
113+
<field name>: Query::<factory method>(<parameters>),
114+
<field name>: Query::<factory method>(<parameters>),
115+
...
116+
);
117+
118+
To combine query criteria by using logical query operators
119+
(``$and``, ``$or``, ``$not``, ``$nor``), you can use the following
120+
query template:
121+
122+
.. code-block:: php
123+
124+
$filter = Query::<logical operator>(
125+
Query::query(<field name>: Query::<factory method>(<parameters>)),
126+
Query::query(<field name>: Query::<factory method>(<parameters>)),
127+
...
128+
);
129+
130+
To learn more, see :manual:`Logical Query Operators
131+
</reference/operator/query-logical/>` in the {+mdb-server+} manual.
132+
133+
The following sections provide examples that use builders to create
134+
filter definitions for different operations.
135+
136+
Retrieve Example
137+
~~~~~~~~~~~~~~~~
138+
139+
This example performs the following actions:
140+
141+
- Uses the ``Query::eq()`` factory method to match documents in which
142+
the ``feature_type`` field value is ``'Wrecks - Visible'``
143+
- Uses the ``Query::near()`` factory method to match documents in which
144+
the ``coordinates`` location field is within ``10000`` meters of the
145+
specified coordinates
146+
- Calls the :phpmethod:`MongoDB\Collection::find()`
147+
method to retrieve the matching documents
148+
- Prints the matching documents
149+
150+
.. io-code-block::
151+
:copyable:
152+
153+
.. input:: /includes/builders.php
154+
:start-after: start-find
155+
:end-before: end-find
156+
:language: php
157+
:dedent:
158+
159+
.. output::
160+
:language: json
161+
:visible: false
162+
163+
{"_id":...,"feature_type":"Wrecks - Visible","coordinates":[-79.9137115,9.3390503],...}
164+
{"_id":...,"feature_type":"Wrecks - Visible","coordinates":[-79.9357223,9.3340302],...}
165+
{"_id":...,"feature_type":"Wrecks - Visible","coordinates":[-79.9081268,9.3547792],...}
166+
// Results truncated
167+
168+
To learn more about find operations, see the :ref:`php-retrieve` guide.
169+
170+
Delete Example
171+
~~~~~~~~~~~~~~
172+
173+
This example performs the following actions:
174+
175+
- Uses the ``Query::or()`` factory method to match documents that
176+
satisfy the either of the following query clauses:
177+
178+
- Clause that uses the ``Query::regex()`` factory method to check if
179+
the ``feature_type`` field value contains the string ``'nondangerous'``
180+
181+
- Clause that uses the ``Query::gt()`` factory method to check if the
182+
``depth`` field value is greater than ``10.0``
183+
184+
- Calls the :phpmethod:`MongoDB\Collection::deleteOne()`
185+
method to delete the first matching document
186+
187+
- Prints the number of deleted documents
188+
189+
.. io-code-block::
190+
:copyable:
191+
192+
.. input:: /includes/builders.php
193+
:start-after: start-deleteone
194+
:end-before: end-deleteone
195+
:language: php
196+
:dedent:
197+
198+
.. output::
199+
:language: none
200+
:visible: false
201+
202+
Deleted documents: 1
203+
204+
To learn more about delete operations, see the :ref:`php-write-delete`
205+
guide.
206+
207+
.. _php-builders-update:
208+
209+
Define an Update Document
210+
-------------------------
211+
212+
You can use factory methods from the ``Stage`` builder class to create
213+
update documents. Update documents describe the updates to make to target
214+
documents. To learn more about updating documents, see the
215+
:ref:`php-write-update` guide.
216+
217+
The following steps describe how to create an update document by using
218+
builders:
219+
220+
1. Create a ``Pipeline`` instance.
221+
222+
#. Pass one or more stages by calling methods from the ``Stage`` class
223+
such as ``Stage::set()`` and passing field names and values.
224+
225+
The following code shows the template to define an update by using
226+
builders:
227+
228+
.. code-block:: php
229+
230+
$update = new Pipeline(
231+
Stage::set(<field name>: <value>),
232+
Stage::set(<field name>: <value>),
233+
...
234+
);
235+
236+
This example performs the following actions:
237+
238+
- Uses the ``Query::eq()`` factory method to match documents in which
239+
the ``watlev`` field value is ``'partly submerged at high water'``
240+
- Uses the ``Stage::set()`` method to set the ``year`` field to ``1870``
241+
- Calls the :phpmethod:`MongoDB\Collection::updateOne()`
242+
method to perform the update
243+
- Prints the number of updated documents
244+
245+
.. io-code-block::
246+
:copyable:
247+
248+
.. input:: /includes/builders.php
249+
:start-after: start-updateone
250+
:end-before: end-updateone
251+
:language: php
252+
:dedent:
253+
254+
.. output::
255+
:language: none
256+
:visible: false
257+
258+
Updated documents: 1
259+
260+
.. _php-builders-changestream:
261+
262+
Modify Change Stream Output
263+
---------------------------
264+
265+
You can use factory methods from the ``Stage`` class to modify a change
266+
stream's output by creating a pipeline. To learn more about change
267+
streams, see the :ref:`php-change-streams` guide.
268+
269+
The following steps describe how to create a change stream filter by
270+
using builders:
271+
272+
1. Create an array.
273+
274+
#. Pass one or more ``$match`` stages by calling factory methods from
275+
the ``Stage`` class and the required parameters.
276+
277+
The following code shows the template to modify change stream output by
278+
using builders:
279+
280+
.. code-block:: php
281+
282+
$pipeline = [
283+
Stage::<factory method>(...),
284+
Stage::<factory method>(...),
285+
...
286+
];
287+
288+
You can pass this pipeline to the following methods:
289+
290+
- :phpmethod:`MongoDB\Client::watch()`
291+
- :phpmethod:`MongoDB\Database::watch()`
292+
- :phpmethod:`MongoDB\Collection::watch()`
293+
294+
This example performs the following actions:
295+
296+
- Uses the ``Stage::match()`` method to filter for only change
297+
events for update operations
298+
- Uses the ``Stage::project()`` method to output only the
299+
``operationType``, ``ns`` (namespace), and ``fullDocument`` fields
300+
- Calls the :phpmethod:`MongoDB\Collection::watch()`
301+
method to open the change stream and sets the ``fullDocument`` option
302+
to output the full document after update
303+
- Prints change events as they occur
304+
305+
.. io-code-block::
306+
:copyable:
307+
308+
.. input:: /includes/builders.php
309+
:start-after: start-cs
310+
:end-before: end-cs
311+
:language: php
312+
:dedent:
313+
314+
.. output::
315+
:language: json
316+
:visible: false
317+
318+
{
319+
"_id":...,
320+
"operationType":"update",
321+
"fullDocument":{"_id":...,"feature_type":"Wrecks - Visible",...},
322+
"ns":{"db":"sample_geospatial","coll":"shipwrecks"}
323+
}
324+
325+
To learn more about the information provided by change events, see
326+
:manual:`Change Events </reference/change-events>` in the {+mdb-server+} manual.

0 commit comments

Comments
 (0)