Skip to content

Commit 22965ed

Browse files
authored
DOCSP-46687 - Collation (#153)
1 parent ee9153f commit 22965ed

19 files changed

+270
-161
lines changed

source/databases-collections.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,25 @@ document count. The following example creates a capped collection called
113113
To learn more about capped collections, see :manual:`Capped Collections </core/capped-collections/>`
114114
in the {+mdb-server+} manual.
115115

116+
Collation
117+
~~~~~~~~~
118+
119+
When you create a collection, you can specify a default **collation** for all operations
120+
you perform on the collection.
121+
122+
.. include:: /includes/collation-description.rst
123+
124+
The following example creates the same collection as the previous example,
125+
but with a default collation of ``fr_CA``:
126+
127+
.. code-block:: python
128+
:emphasize-lines: 4
129+
130+
from pymongo.collation import Collation
131+
132+
database = client["test_database"]
133+
database.create_collection("example_collection", collation=Collation(locale='fr_CA'))
134+
116135
Get a List of Collections
117136
-------------------------
118137

source/fundamentals/collations.txt

Lines changed: 0 additions & 149 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
When you create an index, you can specify a default **collation** for all operations
2+
you perform on fields that are included in the index.
3+
4+
.. include:: /includes/collation-description.rst
5+
6+
To use an index with a specified collation, your operation must meet the following criteria:
7+
8+
- The operation uses the same collation as the one specified in the index.
9+
- The operation is covered by the index that contains the collation.
10+
11+
The following example creates the same index as the previous example,
12+
but with a default collation of ``fr_CA``:
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
A collation is a set of language-specific rules for string comparison, such as
2+
for letter case and accent marks.
3+
4+
To specify a collation, create an instance of the ``Collation`` class or a Python dictionary.
5+
For a list of options to pass to the ``Collation`` constructor or include as keys in the
6+
dictionary, see :manual:`Collation </reference/collation/>` in the {+mdb-server+} manual.
7+
8+
.. tip:: Import Collation
9+
10+
To create an instance of the ``Collation`` class, you must import it from
11+
``pymongo.collation``.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.. note:: Operation Collation Overrides Default
2+
3+
When you specify a collation as part of an operation, it overrides the default
4+
collation for the collection.

source/includes/indexes/indexes.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
movies.create_index("title")
33
# end-index-single
44

5+
# start-index-single-collation
6+
from pymongo.collation import Collation
7+
8+
movies.create_index("title", collation=Collation(locale='fr_CA'))
9+
# end-index-single-collation
10+
511
# start-index-single-query
612
query = { "title": "Batman" }
713
sort = [("title", 1)]
@@ -13,6 +19,13 @@
1319
movies.create_index([("type", pymongo.ASCENDING), ("genre", pymongo.ASCENDING)])
1420
# end-compound-index
1521

22+
# start-compound-index-collation
23+
from pymongo.collation import Collation
24+
25+
movies.create_index([("type", pymongo.ASCENDING), ("genre", pymongo.ASCENDING)],
26+
collation=Collation(locale='fr_CA'))
27+
# end-compound-index-collation
28+
1629
# start-index-compound-query
1730
query = { "type": "movie", "genre": "Drama" }
1831
sort = [("type", pymongo.ASCENDING), ("genre", pymongo.ASCENDING)]
@@ -24,6 +37,12 @@
2437
result = movies.create_index("cast")
2538
# end-index-multikey
2639

40+
# start-index-multikey-collation
41+
from pymongo.collation import Collation
42+
43+
result = movies.create_index("cast", collation=Collation(locale='fr_CA'))
44+
# end-index-multikey-collation
45+
2746
# start-index-multikey-query
2847
query = { "cast": "Viola Davis" }
2948

@@ -36,17 +55,29 @@
3655
)
3756
# end-index-text-single
3857

58+
# start-index-text-single-collation
59+
from pymongo.collation import Collation
60+
61+
movies.create_index(
62+
[( "plot", "text" )],
63+
collation=Collation(locale='fr_CA')
64+
)
65+
# end-index-text-single-collation
66+
3967
# start-index-text-single-query
4068
query = { "$text": { "$search": "a time-traveling DeLorean" } }
4169

4270
cursor = movies.find(query)
4371
# end-index-text-single-query
4472

4573
# start-index-text-multi
74+
from pymongo.collation import Collation
75+
4676
result = myColl.create_index(
4777
[("title", "text"), ("genre", "text")],
4878
default_language="english",
49-
weights={ "title": 10, "genre": 3 }
79+
weights={ "title": 10, "genre": 3 },
80+
collation=Collation(locale='fr_CA')
5081
)
5182
# end-index-text-multi
5283

@@ -56,14 +87,31 @@
5687
)
5788
# end-index-geo
5889

90+
# start-index-geo-collation
91+
from pymongo.collation import Collation
92+
93+
theaters.create_index(
94+
[( "location.geo", "2dsphere" )],
95+
collation=Collation(locale='fr_CA'))
96+
# end-index-geo-collation
97+
5998
# start-index-wildcard
6099
movies.create_index({ "location.$**": pymongo.ASCENDING })
61100
# end-index-wildcard
62101

102+
# start-index-wildcard-collation
103+
movies.create_index({ "location.$**": pymongo.ASCENDING },
104+
collation=Collation(locale='fr_CA'))
105+
# end-index-wildcard-collation
106+
63107
# start-index-unique
64108
theaters.create_index("theaterId", unique=True)
65109
# end-index-unique
66110

111+
# start-index-unique-collation
112+
theaters.create_index("theaterId", unique=True, collation=Collation(locale='fr_CA'))
113+
# end-index-unique-collation
114+
67115
# start-index-clustered
68116
sample_mflix.create_collection("movies", clusteredIndex={
69117
"key": { "_id": 1 },

source/includes/write/delete.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
result = restaurants.delete_many(query_filter)
1111
# end-delete-many
1212

13+
# start-delete-many-collation
14+
from pymongo.collation import Collation
15+
16+
query_filter = { "borough": "Brooklyn" }
17+
18+
result = restaurants.delete_many(query_filter, collation=Collation(locale='fr_CA'))
19+
# end-delete-many-collation
20+
1321
# start-delete-options
1422
query_filter = { 'name': {'$regex': 'Mongo' }}
1523

source/includes/write/update.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@
2020
result = restaurants.update_many(query_filter, update_operation)
2121
# end-update-many
2222

23+
# start-update-many-collation
24+
from pymongo.collation import Collation
25+
26+
restaurants = database["restaurants"]
27+
28+
query_filter = {'cuisine' : 'Pizza'}
29+
update_operation = { '$set' :
30+
{ 'cuisine' : 'Pasta' }
31+
}
32+
33+
result = restaurants.update_many(query_filter, update_operation,
34+
collation=Collation(locale='fr_CA'))
35+
# end-update-many-collation
36+
2337
# start-update-options
2438
restaurants = database["restaurants"]
2539

source/indexes.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ Create Search Index
102102
:language: python
103103
:copyable:
104104

105-
To learn more about creating serach indexes, see the :ref:`pymongo-atlas-search-index-create`
105+
To learn more about creating search indexes, see the :ref:`pymongo-atlas-search-index-create`
106106
guide.
107107

108108
List Search Indexes

source/indexes/compound-index.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,14 @@ the preceding code example:
5050
:end-before: end-index-compound-query
5151

5252
For more information, see :manual:`Compound Indexes </core/index-compound>` in
53-
the {+mdb-server+} manual.
53+
the {+mdb-server+} manual.
54+
55+
Collation
56+
~~~~~~~~~
57+
58+
.. include:: /includes/collation-description-indexes.rst
59+
60+
.. literalinclude:: /includes/indexes/indexes.py
61+
:language: python
62+
:start-after: start-compound-index-collation
63+
:end-before: end-compound-index-collation

source/indexes/geospatial-index.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,13 @@ MongoDB also supports ``2d`` indexes for calculating distances on a Euclidean pl
7575
coordinate pairs" syntax used in MongoDB 2.2 and earlier. For more information,
7676
see the :manual:`Geospatial Queries guide </geospatial-queries>` in the MongoDB
7777
Server manual.
78+
79+
Collation
80+
~~~~~~~~~
81+
82+
.. include:: /includes/collation-description-indexes.rst
83+
84+
.. literalinclude:: /includes/indexes/indexes.py
85+
:language: python
86+
:start-after: start-index-geo-collation
87+
:end-before: end-index-geo-collation

0 commit comments

Comments
 (0)