-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathcollations.txt
313 lines (227 loc) · 9.97 KB
/
collations.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
.. _golang-collations:
==========
Collations
==========
.. meta::
:description: Learn how to use collations in MongoDB to order query results by string values according to specific language and locale conventions.
.. contents:: On this page
:local:
:backlinks: none
:depth: 2
:class: singlecol
Overview
--------
In this guide, you can learn how to use **collations** to order your query
or aggregation operation results by string values. A collation is a set of character
ordering conventions that apply to a specific language and locale.
Collations in MongoDB
---------------------
MongoDB sorts strings using *binary collation* by default. This collation
method uses the `ASCII standard <https://en.wikipedia.org/wiki/ASCII>`_
character values to compare and order strings. Certain languages and locales
have specific character ordering conventions that differ from the ASCII
standard.
For example, in Canadian French, the right-most accented character determines
the ordering for strings when the other characters are the same. Consider the
following Canadian French words:
- cote
- coté
- côte
- côté
When using the default binary collation, MongoDB sorts them in the following order:
.. code-block:: none
cote
coté
côte
côté
When using the Canadian French collation, MongoDB sorts them in the following order:
.. code-block:: none
cote
côte
coté
côté
Specify a Collation
-------------------
To specify a collation, create a ``Collation`` object. You must define the ``Locale`` field
of the ``Collation`` object; all other fields are optional. For example, the following code
example specifies a ``Collation`` object with the ``"en_US"`` locale collation:
.. code-block:: go
myCollation := &options.Collation{Locale: "en_US"}
For a complete list of ``Collation`` object fields, visit the `Collation API documentation
<{+api+}/mongo/options#Collation>`__. To see all the supported locales and the
default values for the ``Locale`` fields, visit :manual:`Supported Languages and Locales
</reference/collation-locales-defaults/#supported-languages-and-locales>`.
Set a Collation on a Collection or View
---------------------------------------
You can apply a collation when you create a new collection or view. This defines the default
collation for any operations called on that collection or view. Set a collation through a
``CreateCollectionOptions`` or ``CreateViewOptions`` object. Then, call the
``CreateCollection()`` or ``CreateView()`` method with your options object as an argument.
.. _golang-create-collection:
Create a Collection Example
~~~~~~~~~~~~~~~~~~~~~~~~~~~
The following example creates a new collection called ``books`` and specifies a default
collation with the ``"fr"`` locale. The ``Strength`` collation field has a value of ``1``
to ignore differences in letter accents.
.. code-block:: go
myCollation := &options.Collation{Locale: "fr", Strength: 1}
opts := options.CreateCollection().SetCollation(myCollation)
err := db.CreateCollection(context.TODO(), "books", opts)
if err != nil {
panic(err)
}
Use the Default Collation Example
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If you call an operation that uses a collation on the ``books`` collection, the operation
uses the default collation specified in the :ref:`golang-create-collection`.
Assume the ``books`` collection contains the following documents:
.. code-block:: json
{"name" : "Emma", "length" : "474"}
{"name" : "Les Misérables", "length": "1462"}
{"name" : "Infinite Jest", "length" : "1104"}
{"name" : "Cryptonomicon", "length" : "918"}
{"name" : "Ça", "length" : "1138"}
.. note::
To learn how to insert documents, see :ref:`golang-insert-guide`.
The following example uses the ``Find()`` method to return all documents with a ``name`` value
that alphabetically precedes ``"Infinite Jest"``:
.. io-code-block::
:copyable: true
.. input::
:language: go
filter := bson.D{{"name", bson.D{{"$lt", "Infinite Jest"}}}}
cursor, err := coll.Find(context.TODO(), filter)
if err != nil {
panic(err)
}
var results []bson.D
if err = cursor.All(context.TODO(), &results); err != nil {
panic(err)
}
for _, result := range results {
res, _ := bson.MarshalExtJSON(result, false, false)
fmt.Println(string(res))
}
.. output::
:language: none
:visible: false
{"name":"Emma","length":"474"}
{"name":"Cryptonomicon","length":"918"}
{"name":"Ça","length":"1138"}
Without specifying a default ``books`` collation, the ``Find()`` method would follow default
binary collation rules to determine the ``name`` values that precede ``"Infinite Jest"``. These
rules place words beginning with "Ç" after those beginning with "I". The output would resemble
the following:
.. code-block:: json
:copyable: false
{"name":"Emma","length":"474"}
{"name":"Cryptonomicon","length":"918"}
To learn more about the ``Find()`` method, see :ref:`golang-retrieve`.
.. _golang-index-collation:
Set a Collation on an Index
---------------------------
You can apply a collation when you create a new index on a collection. The index stores
an ordered representation of the documents in the collection, so your MongoDB instance
doesn't perform the ordering for sorting operations in-memory.
To use the index in an operation, your operation must use the same collation as the one
specified in the index. Additionally, ensure that the operation is covered by the index that
contains the collation. Set a collation through an ``IndexOptions`` object and pass this object
as an argument to the ``CreateOne()`` method.
Example
~~~~~~~
After creating the ``books`` collection and applying a default collation, as shown in the
:ref:`golang-create-collection` section, you cannot change the collection's default collation.
However, you can create an index for the collection with a different collation.
The following example uses the ``CreateOne()`` method to create an ascending index on the
``name`` field and specifies a new collation with an ``"en_US"`` locale:
.. io-code-block::
:copyable: true
.. input::
:language: go
myCollation := &options.Collation{Locale: "en_US"}
opts := options.Index().SetCollation(myCollation)
indexModel := mongo.IndexModel{
Keys: bson.D{{"name", 1}},
Options: opts,
}
name, err := coll.Indexes().CreateOne(context.TODO(), indexModel)
if err != nil {
panic(err)
}
fmt.Println("Name of Index Created: " + name)
.. output::
:language: none
:visible: false
Name of Index Created: name_1
.. _golang-op-collation:
Set a Collation on an Operation
-------------------------------
Operations that read, update, and delete documents from a collection can use collations.
Applying a collation to an operation overrides any default collation previously defined
for a collection.
If you apply a new collation to an operation that differs from an index's collation,
you cannot use that index. As a result, the operation may not perform as well as one
that is covered by an index. For more information on the disadvantages of sorting operations
not covered by an index, see :manual:`Using Indexes to Sort Query Results </tutorial/sort-results-with-indexes/>`.
See the :manual:`MongoDB manual </reference/collation/#collation-document>` for a list of
operations that support collation.
Example
~~~~~~~
You can use operations that support collation to update and query documents in the
``books`` collection.
The following example uses the ``Find()`` method to return documents in which the ``length``
value is greater than ``"1000"``. The ``NumericOrdering`` collation field has a value of
``true`` to ensure that values are sorted in numerical order rather than alphabetical
order:
.. io-code-block::
:copyable: true
.. input::
:language: go
filter := bson.D{{"length", bson.D{{"$gt", "1000"}}}}
myCollation := &options.Collation{Locale: "en_US", NumericOrdering: true}
opts := options.Find().SetCollation(myCollation)
cursor, err := coll.Find(context.TODO(), filter, opts)
if err != nil {
panic(err)
}
var results []bson.D
if err = cursor.All(context.TODO(), &results); err != nil {
panic(err)
}
for _, result := range results {
res, _ := bson.MarshalExtJSON(result, false, false)
fmt.Println(string(res))
}
.. output::
:language: none
:visible: false
{"name":"Les Misérables","length":"1462"}
{"name":"Infinite Jest","length":"1104"}
{"name":"Ça","length":"1138"}
Without specifying a collation with a ``NumericOrdering`` field set to ``true``, the
same ``Find()`` operation compares ``length`` values as strings. In this case, the
output resembles the following:
.. code-block:: json
:copyable: false
{"name":"Emma","length":"474"}
{"name":"Les Misérables","length":"1462"}
{""name":"Infinite Jest","length":"1104"}
{"name":"Cryptonomicon","length":"918"}
{"name":"Ça","length":"1138"}
Additional Information
----------------------
To learn more about the ``Find()`` method, see the :ref:`golang-retrieve` guide.
To learn more about collations, visit the following manual pages:
- :manual:`Collation </reference/collation/#collation-document>`
- :manual:`Collation Locales and Default Parameters </reference/collation-locales-defaults/#supported-languages-and-locales>`
API Documentation
~~~~~~~~~~~~~~~~~
To learn more about the methods discussed in this guide, see the following
API Documentation:
- `Collation <{+api+}/mongo/options#Collation>`__
- `CreateCollectionOptions <{+api+}/mongo/options#CreateCollectionOptions>`__
- `IndexModel <{+api+}/mongo#IndexModel>`__
- `CreateOne() <{+api+}/mongo#IndexView.CreateOne>`__
- `IndexOptions <{+api+}/mongo/options#IndexOptions>`__
- `UpdateOneOptions <{+api+}/mongo/options#UpdateOneOptions>`__
- `UpdateManyOptions <{+api+}/mongo/options#UpdateManyOptions>`__