Skip to content

Commit b42621a

Browse files
jordan-smith721mongoKart
authored andcommitted
DOCSP-38493 Index Code Examples (#74)
* first pass * feedback
1 parent b7c4e7e commit b42621a

File tree

8 files changed

+90
-40
lines changed

8 files changed

+90
-40
lines changed

source/includes/indexes/indexes.py

Lines changed: 79 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,137 @@
11
# start-index-single
2-
2+
movies.create_index("title")
33
# end-index-single
44

55
# start-index-single-query
6+
query = { "title": "Batman" }
7+
sort = [("title", 1)]
68

9+
cursor = movies.find(query).sort(sort)
710
# end-index-single-query
811

912
# start-compound-index
10-
13+
movies.create_index([("type", pymongo.ASCENDING), ("genre", pymongo.ASCENDING)])
1114
# end-compound-index
1215

1316
# start-index-compound-query
17+
query = { "type": "movie", "genre": "Drama" }
18+
sort = [("type", pymongo.ASCENDING), ("genre", pymongo.ASCENDING)]
1419

20+
cursor = movies.find(query).sort(sort)
1521
# end-index-compound-query
1622

1723
# start-index-multikey
18-
24+
result = movies.create_index("cast")
1925
# end-index-multikey
2026

2127
# start-index-multikey-query
28+
query = { "cast": "Viola Davis" }
2229

30+
cursor = movies.find(query)
2331
# end-index-multikey-query
2432

2533
# start-index-text-single
26-
34+
movies.create_index(
35+
[( "plot", "text" )]
36+
)
2737
# end-index-text-single
2838

2939
# start-index-text-single-query
40+
query = { "$text": { "$search": "a time-traveling DeLorean" } }
3041

42+
cursor = movies.find(query)
3143
# end-index-text-single-query
3244

3345
# start-index-text-multi
34-
46+
result = myColl.create_index(
47+
[("title", "text"), ("genre", "text")],
48+
default_language="english",
49+
weights={ "title": 10, "genre": 3 }
50+
)
3551
# end-index-text-multi
3652

37-
# start-index-text-multi-query
38-
39-
# end-index-text-multi-query
40-
4153
# start-index-geo
42-
54+
theaters.create_index(
55+
[( "location.geo", "2dsphere" )]
56+
)
4357
# end-index-geo
4458

45-
# start-index-geo-query
46-
47-
# end-index-geo-query
48-
4959
# start-index-wildcard
50-
60+
movies.create_index({ "location.$**": pymongo.ASCENDING })
5161
# end-index-wildcard
5262

53-
# start-index-clustered
63+
# start-index-unique
64+
theaters.create_index("theaterId", unique=True)
65+
# end-index-unique
5466

67+
# start-index-clustered
68+
sample_mflix.create_collection("movies", clusteredIndex={
69+
"key": { "_id": 1 },
70+
"unique": True
71+
})
5572
# end-index-clustered
5673

5774
# start-remove-index
58-
75+
movies.drop_index("_title_")
5976
# end-remove-index
6077

6178
# start-create-search-index
62-
79+
index = {
80+
"definition": {
81+
"mappings": {
82+
"dynamic": True
83+
}
84+
},
85+
"name": "<index name>",
86+
}
87+
88+
collection.create_search_index(index)
6389
# end-create-search-index
6490

6591
# start-create-search-indexes
66-
92+
index_one = {
93+
"definition": {
94+
"mappings": {
95+
"dynamic": True
96+
}
97+
},
98+
"name": "my_index",
99+
}
100+
101+
index_two = {
102+
"definition": {
103+
"mappings": {
104+
"dynamic": True
105+
}
106+
},
107+
"name": "my_other_index",
108+
}
109+
110+
indexes = [index_one, index_two]
111+
112+
collection.create_search_indexes(models=indexes)
67113
# end-create-search-indexes
68114

69115
# start-list-search-indexes
116+
results = list(collection.list_search_indexes())
70117

118+
for index in results:
119+
print(index)
71120
# end-list-search-indexes
72121

73122
# start-update-search-indexes
74-
123+
new_index = {
124+
"definition": {
125+
"mappings": {
126+
"dynamic": True
127+
}
128+
},
129+
"name": "my_new_index",
130+
}
131+
132+
collection.update_search_index("my_index", new_index)
75133
# end-update-search-indexes
76134

77135
# start-delete-search-indexes
78-
136+
collection.drop_index("my_index")
79137
# end-delete-search-indexes

source/indexes/clustered-index.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ you create your collection:
3030
Sample Data
3131
~~~~~~~~~~~
3232

33-
The examples in this guide use the ``sample_mflix.movies`` collection
33+
The examples in this guide use the ``sample_mflix`` database
3434
from the :atlas:`Atlas sample datasets </sample-data>`. To learn how to create a
3535
free MongoDB Atlas cluster and load the sample datasets, see the
3636
:ref:`<pymongo-get-started>`.
@@ -39,7 +39,7 @@ Create a Clustered Index
3939
------------------------
4040

4141
The following example creates a clustered index on the ``_id`` field in
42-
the ``vendors`` collection:
42+
a new ``movie_reviews`` collection:
4343

4444
.. literalinclude:: /includes/indexes/indexes.py
4545
:language: python

source/indexes/compound-index.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ free MongoDB Atlas cluster and load the sample datasets, see the
3434
Create a Compound Index
3535
-----------------------
3636

37-
The following example creates a compound index on the ``type`` and ``rated`` fields:
37+
The following example creates a compound index on the ``type`` and ``genre`` fields:
3838

3939
.. literalinclude:: /includes/indexes/indexes.py
4040
:language: python

source/indexes/geospatial-index.txt

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Server manual.
3131
Sample Data
3232
~~~~~~~~~~~
3333

34-
The examples in this guide use the ``sample_mflix.movies`` collection
34+
The examples in this guide use the ``sample_mflix.theaters`` collection
3535
from the :atlas:`Atlas sample datasets </sample-data>`. To learn how to create a
3636
free MongoDB Atlas cluster and load the sample datasets, see the
3737
:ref:`<pymongo-get-started>`.
@@ -71,13 +71,6 @@ The following example creates a ``2dsphere`` index on the ``location.geo`` field
7171
:start-after: start-index-geo
7272
:end-before: end-index-geo
7373

74-
The following is an example of a geospatial query using the "location.geo" index.
75-
76-
.. literalinclude:: /includes/indexes/indexes.py
77-
:language: python
78-
:start-after: start-index-geo-query
79-
:end-before: end-index-geo-query
80-
8174
MongoDB also supports ``2d`` indexes for calculating distances on a Euclidean plane and for working with the "legacy
8275
coordinate pairs" syntax used in MongoDB 2.2 and earlier. For more information,
8376
see the :manual:`Geospatial Queries guide </geospatial-queries>` in the MongoDB

source/indexes/multikey-index.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ free MongoDB Atlas cluster and load the sample datasets, see the
3434
Create a Multikey Index
3535
-----------------------
3636

37-
The following example creates a multikey index on the ``rated``, ``genres``, and
38-
``title`` fields:
37+
The following example creates a multikey index on the ``cast`` field:
3938

4039
.. literalinclude:: /includes/indexes/indexes.py
4140
:language: python

source/indexes/single-field-index.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ free MongoDB Atlas cluster and load the sample datasets, see the
4040
Create Single-Field Index
4141
-------------------------
4242

43-
The following example creates an index in ascending order on the ``name`` field:
43+
The following example creates an index in ascending order on the ``title`` field:
4444

4545
.. literalinclude:: /includes/indexes/indexes.py
4646
:start-after: start-index-single

source/indexes/unique-index.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ of a collection. To create a unique index, perform the following steps:
3030
Sample Data
3131
~~~~~~~~~~~
3232

33-
The examples in this guide use the ``sample_mflix.movies`` collection
33+
The examples in this guide use the ``sample_mflix.theaters`` collection
3434
from the :atlas:`Atlas sample datasets </sample-data>`. To learn how to create a
3535
free MongoDB Atlas cluster and load the sample datasets, see the
3636
:ref:`<pymongo-get-started>`.
@@ -42,8 +42,8 @@ The following example creates a descending unique index on the ``theaterId`` fie
4242

4343
.. literalinclude:: /includes/indexes/indexes.py
4444
:language: python
45-
:start-after: start-index-geo-query
46-
:end-before: end-index-geo-query
45+
:start-after: start-index-unique
46+
:end-before: end-index-unique
4747

4848
For more information, see the :manual:`Unique Indexes </core/index-unique>` guide
4949
in the MongoDB Server manual.

source/work-with-indexes.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ Remove a Single Index
104104
Pass an instance of an index or the index name to the ``drop_index()`` method to
105105
remove an index from a collection.
106106

107-
The following example removes an ascending index on the ``title`` field
108-
in a collection:
107+
The following example removes an index with the name ``"_title_"`` from the ``movies``
108+
collection:
109109

110110
.. literalinclude:: /includes/indexes/indexes.py
111111
:language: python

0 commit comments

Comments
 (0)