1
- .. uses collations.rst
1
+ .. _pymongo- collations:
2
2
3
3
Collations
4
4
==========
5
5
6
- .. seealso:: The API docs for ``~pymongo.collation``.
6
+ .. contents:: On this page
7
+ :local:
8
+ :backlinks: none
9
+ :depth: 1
10
+ :class: singlecol
7
11
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
9
20
to use when comparing strings that comply with the conventions of a particular
10
21
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
12
23
ordering rules, and collations allow users to build applications that adhere to
13
- language-specific comparison rules.
24
+ these rules.
14
25
15
26
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:
17
28
18
- .. code-block:: python
29
+ .. code-block:: none
19
30
20
- cote < côte < coté < côté
31
+ cote < côte < coté < côté
21
32
22
33
Specifying a French collation allows users to sort string fields using the
23
34
French sort order.
24
35
25
36
Usage
26
37
-----
27
38
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>`.
32
41
33
42
Collation Parameters:
34
43
~~~~~~~~~~~~~~~~~~~~~
35
44
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:
38
47
39
48
.. code-block:: python
40
49
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>)
49
58
50
59
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/>`__ .
52
61
For example, set ``locale`` to ``en_US`` to represent US English
53
62
or ``fr_CA`` to represent Canadian French.
54
63
55
- For a complete description of the available parameters, see the MongoDB `manual
56
- </>`_.
57
-
58
- .. COMMENT add link for manual entry.
59
-
60
64
.. _collation-on-collection:
61
65
62
66
Assign a Default Collation to a Collection
@@ -66,16 +70,16 @@ The following example demonstrates how to create a new collection called
66
70
``contacts`` and assign a default collation with the ``fr_CA`` locale. This
67
71
operation ensures that all queries that are run against the ``contacts``
68
72
collection use the ``fr_CA`` collation unless another collation is explicitly
69
- specified:
73
+ specified.
70
74
71
75
.. code-block:: python
72
76
73
- from pymongo import MongoClient
74
- from pymongo.collation import Collation
77
+ from pymongo import MongoClient
78
+ from pymongo.collation import Collation
75
79
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'))
79
83
80
84
.. _collation-on-index:
81
85
@@ -86,63 +90,60 @@ When creating a new index, you can specify a default collation.
86
90
87
91
The following example shows how to create an index on the ``name``
88
92
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``.
90
94
91
95
.. code-block:: python
92
96
93
- from pymongo import MongoClient
94
- from pymongo.collation import Collation
97
+ from pymongo import MongoClient
98
+ from pymongo.collation import Collation
95
99
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'))
100
104
101
105
.. _collation-on-operation:
102
106
103
107
Specify a Collation for a Query
104
108
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
105
109
106
- Individual queries can specify a collation to use when sorting
110
+ A query can specify a collation to use when sorting
107
111
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
109
113
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.
111
115
112
116
.. code-block:: python
113
117
114
- from pymongo import MongoClient
115
- from pymongo.collation import Collation
118
+ from pymongo import MongoClient
119
+ from pymongo.collation import Collation
116
120
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'))
120
124
121
125
Other Query Types
122
126
~~~~~~~~~~~~~~~~~
123
127
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
130
131
languages and variants available to the ``locale`` parameter.
131
132
132
133
The following example uses a collation with ``strength`` set to
133
134
``~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.
137
138
138
139
.. code-block:: python
139
140
140
- from pymongo import MongoClient
141
- from pymongo.collation import Collation, CollationStrength
141
+ from pymongo import MongoClient
142
+ from pymongo.collation import Collation, CollationStrength
142
143
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