Skip to content

Commit e3d3314

Browse files
committed
Merge remote-tracking branch 'upstream/comp-cov' into DOCSP-47063-logging
2 parents ee302ce + 1aea063 commit e3d3314

File tree

16 files changed

+1150
-543
lines changed

16 files changed

+1150
-543
lines changed

config/redirects

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ raw: ${prefix}/stable -> ${base}/current/
2525
# redirects in standardized docs
2626
[v1.20-*]: ${prefix}/${version}/tutorial/install-php-library/ -> ${base}/${version}/get-started/
2727
[v1.20-*]: ${prefix}/${version}/tutorial/connecting/ -> ${base}/${version}/connect/
28-
[v1.20-*]: ${prefix}/${version}/tutorial/server-selection/ -> ${base}/${version}/monitoring/cluster-monitoring/
28+
[v1.20-*]: ${prefix}/${version}/tutorial/server-selection/ -> ${base}/${version}/monitoring-logging/monitoring/
2929
[v1.20-*]: ${prefix}/${version}/tutorial/crud/ -> ${base}/${version}/read/
3030
[v1.20-*]: ${prefix}/${version}/tutorial/codecs/ -> ${base}/${version}/data-formats/codecs/
3131
[v1.20-*]: ${prefix}/${version}/tutorial/collation/ -> ${base}/${version}/
@@ -45,7 +45,7 @@ raw: ${prefix}/stable -> ${base}/current/
4545
# note: this mapping does not account for all of the new pages
4646
[*-v1.19]: ${prefix}/${version}/tutorial/install-php-library/ -> ${base}/v1.x/get-started/
4747
[*-v1.19]: ${prefix}/${version}/tutorial/connecting/ -> ${base}/v1.x/connect/
48-
[*-v1.19]: ${prefix}/${version}/tutorial/server-selection/ -> ${base}/v1.x/monitoring/cluster-monitoring/
48+
[*-v1.19]: ${prefix}/${version}/tutorial/server-selection/ -> ${base}/v1.x/monitoring-logging/monitoring/
4949
[*-v1.19]: ${prefix}/${version}/tutorial/crud/ -> ${base}/v1.x/read/
5050
[*-v1.19]: ${prefix}/${version}/tutorial/codecs/ -> ${base}/v1.x/data-formats/codecs/
5151
[*-v1.19]: ${prefix}/${version}/tutorial/collation/ -> ${base}/v1.x/
@@ -84,7 +84,7 @@ raw: ${prefix}/stable -> ${base}/current/
8484
[*-master]: ${prefix}/${version}/data-formats/codecs/ -> ${base}/${version}/data-formats/custom-types/codecs/
8585
[*-master]: ${prefix}/${version}/databases-collections/time-series/ -> ${base}/${version}/data-formats/time-series/
8686
[*-master]: ${prefix}/${version}/read/change-streams/ -> ${base}/${version}/monitoring-logging/change-streams/
87-
[*-master]: ${prefix}/${version}/monitoring/cluster-monitoring/ -> ${base}/${version}/monitoring-logging/cluster-monitoring/
87+
[*-master]: ${prefix}/${version}/monitoring/cluster-monitoring/ -> ${base}/${version}/monitoring-logging/monitoring/
8888
[*-master]: ${prefix}/${version}/compatibility/ -> ${base}/${version}/references/compatibility/
8989
[*-master]: ${prefix}/${version}/whats-new/ -> ${base}/${version}/references/release-notes/
9090
[*-master]: ${prefix}/${version}/upgrade/ -> ${base}/${version}/references/upgrade/

snooty.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ toc_landing_pages = [
2828
"/indexes",
2929
"/security",
3030
"/upgrade",
31+
"/security/authentication",
3132
]
3233

3334
sharedinclude_root = "https://raw.githubusercontent.com/10gen/docs-shared/main/"

source/connect/connection-options.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ Authentication Options
248248
To learn about the authentication options available in the {+driver-short+}, see
249249
:ref:`Authentication Mechanisms. <php-auth>`
250250

251+
.. _php-selection-discovery-options:
252+
251253
Server Selection and Discovery Options
252254
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
253255

source/crud/read-write-pref.txt

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ This section shows how to further customize your read operation
208208
settings in the following ways:
209209

210210
- :ref:`Apply a tag set <php-tag-sets>`
211-
- :ref:`Specify a local threshold <php-local-threshold>`
211+
- :ref:`Configure load balancing behavior <php-load-balancing>`
212212

213213
.. _php-tag-sets:
214214

@@ -241,19 +241,51 @@ to prefer reads from secondary replica set members in the following order:
241241
:start-after: start-tag-set
242242
:end-before: end-tag-set
243243

244+
.. _php-load-balancing:
245+
246+
Load Balancing
247+
~~~~~~~~~~~~~~
248+
249+
When connecting to a sharded cluster or a replica set, the {+library-short+} uses
250+
**load balancing** to handle read and write requests. Load balancing allows the library to
251+
distribute these requests across multiple servers to avoid overwhelming
252+
any one server and ensure optimal performance.
253+
254+
When connecting to a sharded cluster, the {+library-short+} determines the closest ``mongos``
255+
instance by calculating which one has the lowest network round-trip time. Then, the library
256+
determines the latency window by adding this ``mongos``'s average round-trip time to the
257+
:ref:`localThresholdMS value <php-local-threshold>`. The library load balances requests
258+
across up to two random ``mongos`` instances that fall within the latency window. For each request,
259+
the library chooses the server with the lower operation load by determining its ``operationCount``
260+
value.
261+
262+
When connecting to a replica set, the {+library-short+} first selects replica set members
263+
according to your read preference. Then, the library follows the same process as
264+
described in the preceding paragraph. After calculating the latency window, the library
265+
selects up to two random replica set members that fall within the window and chooses
266+
the member with the lower ``operationCount`` value to receive the request.
267+
268+
.. tip::
269+
270+
To learn more about load balancing, see :manual:`Sharded Cluster Balancer
271+
</core/sharding-balancer-administration/>` in the {+mdb-server+} manual.
272+
273+
To learn how to customize the library's server selection behavior, see
274+
:ref:`php-selection-discovery-options` in the Specify Connection Options guide.
275+
244276
.. _php-local-threshold:
245277

246278
Local Threshold
247-
~~~~~~~~~~~~~~~
279+
```````````````
248280

249-
If multiple replica-set members match the read preference and tag sets you specify,
250-
the {+php-library+} reads from the nearest replica-set members, chosen according to
251-
their ping time.
281+
The {+library-short+} uses the local threshold value to calculate the
282+
latency window for server selection. This value determines the servers
283+
that are eligible to receive read and write requests.
252284

253-
By default, the library uses only members whose ping times are within 15 milliseconds
254-
of the nearest member for queries. To distribute reads between members with
255-
higher latencies, pass an options array to the ``MongoDB\Client`` constructor that
256-
sets the ``localThresholdMS`` option.
285+
By default, the library uses only ``mongos`` instances or replica set members whose
286+
ping times are within 15 milliseconds of the nearest server. To
287+
distribute reads among servers with higher latencies, pass an options array to
288+
the ``MongoDB\Client`` constructor that sets the ``localThresholdMS`` option.
257289

258290
The following example specifies a local threshold of 35 milliseconds:
259291

source/data-formats.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Specialized Data Formats
2525
Decimal128 </data-formats/decimal128>
2626
BSON </data-formats/modeling-bson-data>
2727
Time Series </data-formats/time-series>
28+
Extended JSON </data-formats/extended-json>
2829

2930
Overview
3031
--------
@@ -36,4 +37,5 @@ guides:
3637
- :ref:`php-custom-types`
3738
- :ref:`php-decimal128`
3839
- :ref:`php-bson`
39-
- :ref:`php-time-series`
40+
- :ref:`php-time-series`
41+
- :ref:`php-extended-json`

source/data-formats/extended-json.txt

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
.. _php-extended-json:
2+
3+
============================
4+
Work with Extended JSON Data
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: code examples, bson, relaxed, canonical
19+
20+
Overview
21+
--------
22+
23+
In this guide, you can learn how to use the **Extended JSON** data format
24+
when interacting with MongoDB documents.
25+
26+
JSON is a human-readable data format that represents the values of objects,
27+
arrays, numbers, strings, booleans, and nulls. This format supports only a
28+
subset of BSON data types, which is the format that MongoDB uses to store data. The
29+
Extended JSON format supports more BSON types, defining a reserved
30+
set of keys prefixed with "``$``" to represent field type information that
31+
directly corresponds to each type in BSON.
32+
33+
Extended JSON Formats
34+
---------------------
35+
36+
MongoDB Extended JSON features two string formats to represent BSON data.
37+
Each format conforms to the `JSON RFC <https://www.rfc-editor.org/rfc/rfc8259>`__
38+
and meets specific use cases.
39+
40+
The following table describes each Extended JSON format:
41+
42+
.. list-table::
43+
:header-rows: 1
44+
:stub-columns: 1
45+
:widths: 10 40
46+
47+
* - Name
48+
- Description
49+
50+
* - **Canonical** or **Extended**
51+
- | A string format that avoids loss of BSON type information during data conversions.
52+
| This format prioritizes type preservation at the loss of human-readability and
53+
interoperability with older formats.
54+
55+
* - **Relaxed Mode**
56+
- | A string format that describes BSON documents with some type information loss.
57+
| This format prioritizes human-readability and interoperability at the loss of
58+
certain type information.
59+
60+
To learn more about JSON, BSON, and Extended JSON, see the
61+
`JSON and BSON <https://www.mongodb.com/resources/basics/json-and-bson>`__ resource
62+
and :manual:`Extended JSON </reference/mongodb-extended-json/>` {+mdb-server+} manual
63+
entry.
64+
65+
.. _php-extended_json_example:
66+
67+
Extended JSON Examples
68+
~~~~~~~~~~~~~~~~~~~~~~
69+
70+
The following example shows a document containing an ``ObjectId``, date, and long
71+
number field represented in the Extended JSON format. Select the :guilabel:`Canonical`
72+
or the :guilabel:`Relaxed Mode` tab to view the sample document in each Extended
73+
JSON format:
74+
75+
.. tabs::
76+
77+
.. tab:: Canonical
78+
:tabid: canonical-format
79+
80+
.. code-block:: json
81+
82+
{
83+
"_id": { "$oid": "573a1391f29313caabcd9637" },
84+
"createdAt": { "$date": { "$numberLong": "1601499609" }},
85+
"numViews": { "$numberLong": "36520312" }
86+
}
87+
88+
.. tab:: Relaxed Mode
89+
:tabid: relaxed-mode-format
90+
91+
.. code-block:: json
92+
93+
{
94+
"_id": { "$oid": "573a1391f29313caabcd9637" },
95+
"createdAt": { "$date": "2020-09-30T18:22:51.648Z" },
96+
"numViews": 36520312
97+
}
98+
99+
Write Extended JSON
100+
-------------------
101+
102+
You can write an Extended JSON string from a BSON document object by using the
103+
``toRelaxedExtendedJSON()`` and ``toCanonicalExtendedJSON()`` methods.
104+
105+
The following example outputs a BSON document in both Relaxed and Canonical
106+
Extended JSON formats:
107+
108+
.. io-code-block::
109+
:copyable:
110+
111+
.. input:: /includes/extended-json.php
112+
:start-after: start-write-extended
113+
:end-before: end-write-extended
114+
:language: php
115+
:dedent:
116+
117+
.. output::
118+
:visible: false
119+
120+
Relaxed format: { "foo" : [ 1, 2 ], "bar" : { "hello" : "world" }, "code" :
121+
{ "$code" : "function x() { return 1; }", "$scope" : { } }, "date" : { } }
122+
Canonical format: { "foo" : [ { "$numberInt" : "1" }, { "$numberInt" : "2" } ],
123+
"bar" : { "hello" : "world" }, "code" : { "$code" : "function x() { return 1; }",
124+
"$scope" : { } }, "date" : { } }
125+
126+
Read Extended JSON
127+
------------------
128+
129+
You can read an Extended JSON string into a PHP value by calling
130+
the ``json_decode()`` method, which converts Extended JSON to a
131+
PHP array or object. Pass the following arguments to ``json_decode()``:
132+
133+
- Extended JSON string to read.
134+
- Boolean value that indicates whether you want to return
135+
an array value. If set to ``false``, the method returns an
136+
object value.
137+
138+
The following example converts an Extended JSON string value
139+
to a PHP array:
140+
141+
.. io-code-block::
142+
:copyable:
143+
144+
.. input:: /includes/extended-json.php
145+
:start-after: start-read-extended
146+
:end-before: end-read-extended
147+
:language: php
148+
:dedent:
149+
150+
.. output::
151+
:visible: false
152+
153+
Array
154+
(
155+
[foo] => Array
156+
(
157+
[0] => Array
158+
(
159+
[$numberInt] => 1
160+
)
161+
162+
[1] => Array
163+
(
164+
[$numberInt] => 2
165+
)
166+
167+
)
168+
169+
[bar] => Array
170+
(
171+
[hello] => world
172+
)
173+
174+
[code] => Array
175+
(
176+
[$code] => function x() { return 1; }
177+
[$scope] => Array
178+
(
179+
)
180+
181+
)
182+
183+
[bin] => Array
184+
(
185+
[$binary] => Array
186+
(
187+
[base64] => AQIDBA==
188+
[subType] => 00
189+
)
190+
191+
)
192+
193+
)
194+
195+
API Documentation
196+
-----------------
197+
198+
To learn more about the methods discussed on this page, see
199+
the following {+extension-short+} API documentation:
200+
201+
- :php:`MongoDB\BSON\Document::toRelaxedExtendedJSON() <mongodb-bson-document.torelaxedextendedjson>`
202+
- :php:`MongoDB\BSON\Document::toCanonicalExtendedJSON() <mongodb-bson-document.tocanonicalextendedjson>`
203+
- :php:`json_decode() <function.json-decode>`

source/includes/authentication.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22

33
require __DIR__ . '/../vendor/autoload.php';
44

5+
// start-default-client
6+
$uriOptions = [
7+
'username' => '<username>',
8+
'password' => '<password>',
9+
'authSource' => '<authentication database>',
10+
];
11+
12+
$client = new MongoDB\Client(
13+
'mongodb://<hostname>:<port>',
14+
$uriOptions,
15+
);
16+
// end-default-client
17+
18+
// start-default-uri
19+
$uri = 'mongodb://<username>:<password>@<hostname>:<port>/?authSource=admin';
20+
$client = new MongoDB\Client($uri);
21+
// end-default-uri
22+
523
// start-scram-sha-256-client
624
$uriOptions = [
725
'username' => '<username>',

source/includes/extended-json.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
require 'vendor/autoload.php';
4+
5+
// start-write-extended
6+
$doc = [
7+
'foo' => [1, 2],
8+
'bar' => ['hello' => 'world'],
9+
'code' => new MongoDB\BSON\Javascript('function x() { return 1; }', []),
10+
'date' => new DateTime('2024-07-20 10:30:00')
11+
];
12+
13+
echo 'Relaxed format: ' , MongoDB\BSON\Document::fromPHP($doc)->toRelaxedExtendedJSON(), PHP_EOL;
14+
echo 'Canonical format: ' , MongoDB\BSON\Document::fromPHP($doc)->toCanonicalExtendedJSON(), PHP_EOL;
15+
// end-write-extended
16+
17+
// start-read-extended
18+
$ejsonStr = '{
19+
"foo": [
20+
{ "$numberInt": "1" },
21+
{ "$numberInt": "2" }
22+
],
23+
"bar": { "hello": "world" },
24+
"code": {
25+
"$code": "function x() { return 1; }",
26+
"$scope": {}
27+
},
28+
"bin": { "$binary": { "base64": "AQIDBA==", "subType": "00" } }
29+
}';
30+
31+
$decodedJson = json_decode($ejsonStr, true);
32+
print_r($decodedJson);
33+
// end-read-extended

0 commit comments

Comments
 (0)