Skip to content

Commit f0534e3

Browse files
DOCSP-36997 - Collation (#27)
Co-authored-by: Jordan Smith <[email protected]>
1 parent 0357e21 commit f0534e3

File tree

2 files changed

+69
-66
lines changed

2 files changed

+69
-66
lines changed

source/fundamentals.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Fundamentals
1313

1414
/fundamentals/authentication
1515
/fundamentals/csot
16+
/fundamentals/collations
1617
/fundamentals/databases-and-collections
1718
/fundamentals/dates-and-times
1819
/fundamentals/enterprise-authentication
@@ -24,6 +25,7 @@ Fundamentals
2425

2526
- :ref:`pymongo-auth`
2627
- :ref:`pymongo-csot`
28+
- :ref:`pymongo-collations`
2729
- :ref:`pymongo-databases-collections`
2830
- :ref:`pymongo-dates-times`
2931
- :ref:`pymongo-enterprise-auth`

source/fundamentals/collations.txt

Lines changed: 67 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,66 @@
1-
.. uses collations.rst
1+
.. _pymongo-collations:
22

33
Collations
44
==========
55

6-
.. seealso:: The API docs for ``~pymongo.collation``.
6+
.. contents:: On this page
7+
:local:
8+
:backlinks: none
9+
:depth: 1
10+
:class: singlecol
711

8-
Collations are a new feature in MongoDB version 3.4. They provide a set of rules
12+
.. facet::
13+
:name: genre
14+
:values: reference
15+
16+
.. meta::
17+
:keywords: order, sort, translation, accent, diacritic, compare
18+
19+
Collations provide a set of rules
920
to use when comparing strings that comply with the conventions of a particular
1021
language, such as Spanish or German. If no collation is specified, the server
11-
sorts strings based on a binary comparison. Many languages have specific
22+
sorts strings based on a binary comparison. However, many languages have specific
1223
ordering rules, and collations allow users to build applications that adhere to
13-
language-specific comparison rules.
24+
these rules.
1425

1526
In French, for example, the last accent in a given word determines the sorting
16-
order. The correct sorting order for the following four words in French is:
27+
order. The following example shows the correct sorting order for four words in French:
1728

18-
.. code-block:: python
29+
.. code-block:: none
1930

20-
cote < côte < coté < côté
31+
cote < côte < coté < côté
2132

2233
Specifying a French collation allows users to sort string fields using the
2334
French sort order.
2435

2536
Usage
2637
-----
2738

28-
Users can specify a collation for a
29-
:ref:`collection<collation-on-collection>`, an
30-
:ref:`index<collation-on-index>`, or a
31-
:ref:`CRUD command <collation-on-operation>`.
39+
You can specify a collation for a :ref:`collection <collation-on-collection>`, an
40+
:ref:`index<collation-on-index>`, or a :ref:`CRUD command <collation-on-operation>`.
3241

3342
Collation Parameters:
3443
~~~~~~~~~~~~~~~~~~~~~
3544

36-
Collations can be specified with the ``~pymongo.collation.Collation`` model
37-
or with plain Python dictionaries. The structure is the same:
45+
You can specify a collation by using the ``~pymongo.collation.Collation`` model
46+
or Python dictionaries. In either case, the structure is the same:
3847

3948
.. code-block:: python
4049

41-
Collation(locale=<string>,
42-
caseLevel=<bool>,
43-
caseFirst=<string>,
44-
strength=<int>,
45-
numericOrdering=<bool>,
46-
alternate=<string>,
47-
maxVariable=<string>,
48-
backwards=<bool>)
50+
Collation(locale=<string>,
51+
caseLevel=<bool>,
52+
caseFirst=<string>,
53+
strength=<int>,
54+
numericOrdering=<bool>,
55+
alternate=<string>,
56+
maxVariable=<string>,
57+
backwards=<bool>)
4958

5059
The only required parameter is ``locale``, which the server parses as
51-
an `ICU format locale ID <https://www.mongodb.com/docs/manual/reference/collation-locales-defaults/>`_.
60+
an `ICU format locale ID <https://www.mongodb.com/docs/manual/reference/collation-locales-defaults/>`__.
5261
For example, set ``locale`` to ``en_US`` to represent US English
5362
or ``fr_CA`` to represent Canadian French.
5463

55-
For a complete description of the available parameters, see the MongoDB `manual
56-
</>`_.
57-
58-
.. COMMENT add link for manual entry.
59-
6064
.. _collation-on-collection:
6165

6266
Assign a Default Collation to a Collection
@@ -66,16 +70,16 @@ The following example demonstrates how to create a new collection called
6670
``contacts`` and assign a default collation with the ``fr_CA`` locale. This
6771
operation ensures that all queries that are run against the ``contacts``
6872
collection use the ``fr_CA`` collation unless another collation is explicitly
69-
specified:
73+
specified.
7074

7175
.. code-block:: python
7276

73-
from pymongo import MongoClient
74-
from pymongo.collation import Collation
77+
from pymongo import MongoClient
78+
from pymongo.collation import Collation
7579

76-
db = MongoClient().test
77-
collection = db.create_collection('contacts',
78-
collation=Collation(locale='fr_CA'))
80+
db = MongoClient().test
81+
collection = db.create_collection('contacts',
82+
collation=Collation(locale='fr_CA'))
7983

8084
.. _collation-on-index:
8185

@@ -86,63 +90,60 @@ When creating a new index, you can specify a default collation.
8690

8791
The following example shows how to create an index on the ``name``
8892
field of the ``contacts`` collection, with the ``unique`` parameter
89-
enabled and a default collation with ``locale`` set to ``fr_CA``:
93+
enabled and a default collation with ``locale`` set to ``fr_CA``.
9094

9195
.. code-block:: python
9296

93-
from pymongo import MongoClient
94-
from pymongo.collation import Collation
97+
from pymongo import MongoClient
98+
from pymongo.collation import Collation
9599

96-
contacts = MongoClient().test.contacts
97-
contacts.create_index('name',
98-
unique=True,
99-
collation=Collation(locale='fr_CA'))
100+
contacts = MongoClient().test.contacts
101+
contacts.create_index('name',
102+
unique=True,
103+
collation=Collation(locale='fr_CA'))
100104

101105
.. _collation-on-operation:
102106

103107
Specify a Collation for a Query
104108
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
105109

106-
Individual queries can specify a collation to use when sorting
110+
A query can specify a collation to use when sorting
107111
results. The following example demonstrates a query that runs on the
108-
``contacts`` collection in database ``test``. It matches on
112+
``contacts`` collection in the ``test`` database. It matches on
109113
documents that contain ``New York`` in the ``city`` field,
110-
and sorts on the ``name`` field with the ``fr_CA`` collation:
114+
and sorts on the ``name`` field with the ``fr_CA`` collation.
111115

112116
.. code-block:: python
113117

114-
from pymongo import MongoClient
115-
from pymongo.collation import Collation
118+
from pymongo import MongoClient
119+
from pymongo.collation import Collation
116120

117-
collection = MongoClient().test.contacts
118-
docs = collection.find({'city': 'New York'}).sort('name').collation(
119-
Collation(locale='fr_CA'))
121+
collection = MongoClient().test.contacts
122+
docs = collection.find({'city': 'New York'}).sort('name').collation(
123+
Collation(locale='fr_CA'))
120124

121125
Other Query Types
122126
~~~~~~~~~~~~~~~~~
123127

124-
You can use collations to control document matching rules for several different
125-
types of queries. All the various update and delete methods
126-
(the ``~pymongo.collection.Collection.update_one`` method,
127-
the ``~pymongo.collection.Collection.update_many`` method,
128-
the ``~pymongo.collection.Collection.delete_one`` method, etc.) support collation, and
129-
you can create query filters which employ collations to comply with any of the
128+
You can use collations to control document-matching rules for several different
129+
types of queries. All methods that perform update or delete operations support collation,
130+
and you can create query filters that use collations to comply with any of the
130131
languages and variants available to the ``locale`` parameter.
131132

132133
The following example uses a collation with ``strength`` set to
133134
``~pymongo.collation.CollationStrength.SECONDARY``, which considers only
134-
the base character and character accents in string comparisons, but not case
135-
sensitivity, for example. All documents in the ``contacts`` collection with
136-
``jürgen`` (case-insensitive) in the ``first_name`` field are updated:
135+
the base character and character accents in string comparisons, but not case-sensitivity,
136+
for example. All documents in the ``contacts`` collection with
137+
``jürgen`` (case-insensitive) in the ``first_name`` field are updated.
137138

138139
.. code-block:: python
139140

140-
from pymongo import MongoClient
141-
from pymongo.collation import Collation, CollationStrength
141+
from pymongo import MongoClient
142+
from pymongo.collation import Collation, CollationStrength
142143

143-
contacts = MongoClient().test.contacts
144-
result = contacts.update_many(
145-
{'first_name': 'jürgen'},
146-
{'$set': {'verified': 1}},
147-
collation=Collation(locale='de',
148-
strength=CollationStrength.SECONDARY))
144+
contacts = MongoClient().test.contacts
145+
result = contacts.update_many(
146+
{'first_name': 'jürgen'},
147+
{'$set': {'verified': 1}},
148+
collation=Collation(locale='de',
149+
strength=CollationStrength.SECONDARY))

0 commit comments

Comments
 (0)