Skip to content

Commit 90967fc

Browse files
committed
Merge branch 'v1.1'
2 parents a203798 + ff8426c commit 90967fc

File tree

3 files changed

+375
-1
lines changed

3 files changed

+375
-1
lines changed

docs/reference/method/MongoDBCollection-findOneAndUpdate.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ setting its building number to ``"761"``:
6868
[ '$set' => [ 'address.building' => '761' ]],
6969
[
7070
'projection' => [ 'address' => 1 ],
71-
'returnDocument' => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER
71+
'returnDocument' => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER,
7272
]
7373
);
7474

docs/tutorial.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Tutorials
66
.. toctree::
77

88
/tutorial/crud
9+
/tutorial/collation
910
/tutorial/commands
1011
/tutorial/decimal128
1112
/tutorial/gridfs

docs/tutorial/collation.txt

Lines changed: 373 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,373 @@
1+
=========
2+
Collation
3+
=========
4+
5+
.. default-domain:: mongodb
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
.. versionadded:: 1.1
14+
15+
Overview
16+
--------
17+
18+
MongoDB 3.4 introduced support for :manual:`collations
19+
</manual/reference/collation/>`, which provide a set of rules to comply with the
20+
conventions of a particular language when comparing strings.
21+
22+
For example, in Canadian French, the last accent in a given word determines the
23+
sorting order. Consider the following French words:
24+
25+
.. code-block:: none
26+
27+
cote < coté < côte < côté
28+
29+
The sort order using the Canadian French collation would result in the
30+
following:
31+
32+
.. code-block:: none
33+
34+
cote < côte < coté < côté
35+
36+
If collation is unspecified, MongoDB uses simple binary comparison for strings.
37+
As such, the sort order of the words would be:
38+
39+
.. code-block:: none
40+
41+
cote < coté < côte < côté
42+
43+
Usage
44+
-----
45+
46+
You can specify a default collation for collections and indexes when they are
47+
created, or specify a collation for CRUD operations and aggregations. For
48+
operations that support collation, MongoDB uses the collection's default
49+
collation unless the operation specifies a different collation.
50+
51+
Collation Parameters
52+
~~~~~~~~~~~~~~~~~~~~
53+
54+
.. code-block:: php
55+
56+
'collation' => [
57+
'locale' => <string>,
58+
'caseLevel' => <boolean>,
59+
'caseFirst' => <string>,
60+
'strength' => <integer>,
61+
'numericOrdering' => <boolean>,
62+
'alternate' => <string>,
63+
'maxVariable' => <string>,
64+
'normalization' => <boolean>,
65+
'backwards' => <boolean>,
66+
]
67+
68+
The only required parameter is ``locale``, which the server parses as an `ICU
69+
format locale ID <http://userguide.icu-project.org/locale>`_. For example, set
70+
``locale`` to ``en_US`` to represent US English or ``fr_CA`` to represent
71+
Canadian French.
72+
73+
For a complete description of the available parameters, see :manual:`Collation
74+
Document </reference/collation/#collation-document>` in the MongoDB manual.
75+
76+
Assign a Default Collation to a Collection
77+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
78+
79+
The following example creates a new collection called ``contacts`` on the
80+
``test`` database and assigns a default collation with the ``fr_CA`` locale.
81+
Specifying a collation when you create the collection ensures that all
82+
operations involving a query that are run against the ``contacts`` collection
83+
use the ``fr_CA`` collation, unless the query specifies another collation. Any
84+
indexes on the new collection also inherit the default collation, unless the
85+
creation command specifies another collation.
86+
87+
.. code-block:: php
88+
89+
<?php
90+
91+
$database = (new MongoDB\Client)->test;
92+
93+
$database->createCollection('contacts', [
94+
'collation' => ['locale' => 'fr_CA'],
95+
]);
96+
97+
Assign a Collation to an Index
98+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
99+
100+
To specify a collation for an index, use the ``collation`` option when you
101+
create the index.
102+
103+
The following example creates an index on the ``name`` field of the
104+
``address_book`` collection, with the ``unique`` parameter enabled and a default
105+
collation with ``locale`` set to ``en_US``.
106+
107+
.. code-block:: php
108+
109+
<?php
110+
111+
$collection = (new MongoDB\Client)->test->address_book;
112+
113+
$collection->createIndex(
114+
['first_name' => 1],
115+
[
116+
'collation' => ['locale' => 'en_US'],
117+
'unique' => true,
118+
]
119+
);
120+
121+
To use this index, make sure your queries also specify the same collation. The
122+
following query uses the above index:
123+
124+
.. code-block:: php
125+
126+
<?php
127+
128+
$collection = (new MongoDB\Client)->test->address_book;
129+
130+
$cursor = $collection->find(
131+
['first_name' => 'Adam'],
132+
[
133+
'collation' => ['locale' => 'en_US'],
134+
]
135+
);
136+
137+
The following queries do **NOT** use the index. The first query uses no
138+
collation, and the second uses a collation with a different ``strength`` value
139+
than the collation on the index.
140+
141+
.. code-block:: php
142+
143+
<?php
144+
145+
$collection = (new MongoDB\Client)->test->address_book;
146+
147+
$cursor1 = $collection->find(['first_name' => 'Adam']);
148+
149+
$cursor2 = $collection->find(
150+
['first_name' => 'Adam'],
151+
[
152+
'collation' => [
153+
'locale' => 'en_US',
154+
'strength' => 2,
155+
],
156+
]
157+
);
158+
159+
Operations that Support Collation
160+
---------------------------------
161+
162+
All reading, updating, and deleting methods support collation. Some examples are
163+
listed below.
164+
165+
``find()`` with ``sort``
166+
~~~~~~~~~~~~~~~~~~~~~~~~
167+
168+
Individual queries can specify a collation to use when matching and sorting
169+
results. The following query and sort operation uses a German collation with the
170+
``locale`` parameter set to ``de``.
171+
172+
.. code-block:: php
173+
174+
<?php
175+
176+
$collection = (new MongoDB\Client)->test->contacts;
177+
178+
$cursor = $collection->find(
179+
['city' => 'New York'],
180+
[
181+
'collation' => ['locale' => 'de'],
182+
'sort' => ['name' => 1],
183+
]
184+
);
185+
186+
``findOneAndUpdate()``
187+
~~~~~~~~~~~~~~~~~~~~~~
188+
189+
A collection called ``names`` contains the following documents:
190+
191+
.. code-block:: javascript
192+
193+
{ "_id" : 1, "first_name" : "Hans" }
194+
{ "_id" : 2, "first_name" : "Gunter" }
195+
{ "_id" : 3, "first_name" : "Günter" }
196+
{ "_id" : 4, "first_name" : "Jürgen" }
197+
198+
The following :phpmethod:`findOneAndUpdate()
199+
<MongoDB\\Collection::findOneAndUpdate>` operation on the collection does not
200+
specify a collation.
201+
202+
.. code-block:: php
203+
204+
<?php
205+
206+
$collection = (new MongoDB\Client)->test->names;
207+
208+
$document = $collection->findOneAndUpdate(
209+
['first_name' => ['$lt' =-> 'Gunter']],
210+
['$set' => ['verified' => true]]
211+
);
212+
213+
Because ``Gunter`` is lexically first in the collection, the above operation
214+
returns no results and updates no documents.
215+
216+
Consider the same :phpmethod:`findOneAndUpdate()
217+
<MongoDB\\Collection::findOneAndUpdate>` operation but with a collation
218+
specified, which uses the locale ``de@collation=phonebook``.
219+
220+
.. note::
221+
222+
Some locales have a ``collation=phonebook`` option available for use with
223+
languages which sort proper nouns differently from other words. According to
224+
the ``de@collation=phonebook`` collation, characters with umlauts come before
225+
the same characters without umlauts.
226+
227+
.. code-block:: php
228+
229+
<?php
230+
231+
$collection = (new MongoDB\Client)->test->names;
232+
233+
$document = $collection->findOneAndUpdate(
234+
['first_name' => ['$lt' =-> 'Gunter']],
235+
['$set' => ['verified' => true]],
236+
[
237+
'collation' => ['locale' => 'de@collation=phonebook'],
238+
'returnDocument' => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER,
239+
]
240+
);
241+
242+
The operation returns the following updated document:
243+
244+
.. code-block:: javascript
245+
246+
{ "_id" => 3, "first_name" => "Günter", "verified" => true }
247+
248+
``findOneAndDelete()``
249+
~~~~~~~~~~~~~~~~~~~~~~
250+
251+
Set the ``numericOrdering`` collation parameter to ``true`` to compare numeric
252+
strings by their numeric values.
253+
254+
The collection ``numbers`` contains the following documents:
255+
256+
.. code-block:: javascript
257+
258+
{ "_id" : 1, "a" : "16" }
259+
{ "_id" : 2, "a" : "84" }
260+
{ "_id" : 3, "a" : "179" }
261+
262+
The following example matches the first document in which field ``a`` has a
263+
numeric value greater than 100 and deletes it.
264+
265+
.. code-block:: php
266+
267+
<?php
268+
269+
$collection = (new MongoDB\Client)->test->numbers;
270+
271+
$document = $collection->findOneAndDelete(
272+
['a' => ['$gt' =-> '100']],
273+
[
274+
'collation' => [
275+
'locale' => 'en',
276+
'numericOrdering' => true,
277+
],
278+
]
279+
);
280+
281+
After the above operation, the following documents remain in the collection:
282+
283+
.. code-block:: javascript
284+
285+
{ "_id" : 1, "a" : "16" }
286+
{ "_id" : 2, "a" : "84" }
287+
288+
If you perform the same operation without collation, the server deletes the
289+
first document it finds in which the lexical value of ``a`` is greater than
290+
``"100"``.
291+
292+
.. code-block:: php
293+
294+
<?php
295+
296+
$collection = (new MongoDB\Client)->test->numbers;
297+
298+
$document = $collection->findOneAndDelete(['a' => ['$gt' =-> '100']]);
299+
300+
After the above operation is executed, the document in which ``a`` was equal to
301+
``"16"`` has been deleted, and the following documents remain in the collection:
302+
303+
.. code-block:: javascript
304+
305+
{ "_id" : 2, "a" : "84" }
306+
{ "_id" : 3, "a" : "179" }
307+
308+
``deleteMany()``
309+
~~~~~~~~~~~~~~~~
310+
311+
You can use collations with all the various CRUD operations which exist in the
312+
|php-library|.
313+
314+
The collection ``recipes`` contains the following documents:
315+
316+
.. code-block:: javascript
317+
318+
{ "_id" : 1, "dish" : "veggie empanadas", "cuisine" : "Spanish" }
319+
{ "_id" : 2, "dish" : "beef bourgignon", "cuisine" : "French" }
320+
{ "_id" : 3, "dish" : "chicken molé", "cuisine" : "Mexican" }
321+
{ "_id" : 4, "dish" : "chicken paillard", "cuisine" : "french" }
322+
{ "_id" : 5, "dish" : "pozole verde", "cuisine" : "Mexican" }
323+
324+
Setting the ``strength`` parameter of the collation document to ``1`` or ``2``
325+
causes the server to disregard case in the query filter. The following example
326+
uses a case-insensitive query filter to delete all records in which the
327+
``cuisine`` field matches ``French``.
328+
329+
.. code-block:: php
330+
331+
<?php
332+
333+
$collection = (new MongoDB\Client)->test->recipes;
334+
335+
$collection->deleteMany(
336+
['cuisine' => 'French'],
337+
[
338+
'collation' => [
339+
'locale' => 'en_US',
340+
'strength' => 1,
341+
],
342+
]
343+
);
344+
345+
After the above operation runs, the documents with ``_id`` values of ``2`` and
346+
``4`` are deleted from the collection.
347+
348+
Aggregation
349+
~~~~~~~~~~~
350+
351+
To use collation with an :phpmethod:`aggregate()
352+
<MongoDB\\Collection::aggregate>` operation, specify a collation in the
353+
aggregation options.
354+
355+
The following aggregation example uses a collection called ``names`` and groups
356+
the ``first_name`` field together, counts the total number of results in each
357+
group, and sorts the results by German phonebook order.
358+
359+
.. code-block:: php
360+
361+
<?php
362+
363+
$collection = (new MongoDB\Client)->test->recipes;
364+
365+
$cursor = $collection->aggregate(
366+
[
367+
['$group' => ['_id' => '$first_name', 'name_count' => ['$sum' => 1]]],
368+
['$sort' => ['_id' => 1]],
369+
],
370+
[
371+
'collation' => ['locale' => 'de@collation=phonebook'],
372+
]
373+
);

0 commit comments

Comments
 (0)