1+ # start-single-field
2+ result = collection .create_index ("<field name>" )
3+
4+ print (f'Index created: { result } ' )
5+ # end-single-field
6+
7+ # start-compound
8+ result = collection .create_index ([
9+ ("<field name one>" , pymongo .ASCENDING ),
10+ ("<field name two>" , pymongo .ASCENDING )
11+ ])
12+
13+ print (f"Index created: { result } " )
14+ # end-compound
15+
16+ # start-multikey
17+ result = collection .create_index ("<array field name>" )
18+
19+
20+ print (f'Index created: { result } ' )
21+ # end-multikey
22+
23+ # start-search-create
24+ index = {
25+ "definition" : {
26+ "mappings" : {
27+ "dynamic" : True
28+ }
29+ },
30+ "name" : "<index name>" ,
31+ }
32+
33+ collection .create_search_index (index )
34+ # end-search-create
35+
36+ # start-search-list
37+ results = list (collection .list_search_indexes ())
38+
39+ print ('Existing search indexes:\n ' )
40+ for index in results :
41+ print (index )
42+ # end-search-list
43+
44+ # start-search-update
45+ new_index = {
46+ "definition" : {
47+ "mappings" : {
48+ "dynamic" : True
49+ }
50+ },
51+ "name" : "<index name>" ,
52+ }
53+
54+ collection .update_search_index ("<name of index to update>" , new_index )
55+
56+ print (f"Search index updated: { result } " )
57+ # end-search-update
58+
59+ # start-search-drop
60+ collection .drop_index ("<index name>" )
61+
62+ print (f"Search index deleted: { result } " )
63+ # end-search-drop
64+
65+ # start-text
66+ result = collection .create_index (
67+ [( "<field name>" , "text" )],
68+ default_language = "english" ,
69+ weights = { "<field name>" : 10 }
70+ )
71+
72+ print (f"Index created: { result } " )
73+ # end-text
74+
75+ # start-geo
76+ result = collection .create_index ([("<GeoJSON object field>" , "2dsphere" )])
77+
78+ print (f'Index created: { result } ' )
79+ # end-geo
80+
81+ # start-unique
82+ result = collection .create_index ("<field name>" , unique = True )
83+
84+ print (f"Index created: { result } " )
85+ # end-unique
86+
87+ # start-wildcard
88+ result = collection .create_index ({"$**" : pymongo .ASCENDING })
89+
90+ print (f'Index created: { result } ' )
91+ # end-wildcard
92+
93+ # start-clustered
94+ collection = database .create_collection ("<collection name>" , clusteredIndex = {
95+ "key" : {"_id" : 1 },
96+ "unique" : True
97+ })
98+ # end-clustered
99+
100+ # start-remove
101+ collection .drop_index ("<index_name>" )
102+ # end-remove
0 commit comments