@@ -223,6 +223,47 @@ it is often useful for complex migrations of Document models.
223223
224224 .. warning :: Be aware of this `flaw <https://groups.google.com/g/mongodb-user/c/AFC1ia7MHzk>`_ if you modify documents while iterating
225225
226+ Example 4: Index removal
227+ ========================
228+
229+ If you remove an index from your Document class, or remove an indexed Field from your Document class,
230+ you'll need to manually drop the corresponding index. MongoEngine will not do that for you.
231+
232+ The way to deal with this case is to identify the name of the index to drop with `index_information() `, and then drop
233+ it with `drop_index() `
234+
235+ Let's for instance assume that you start with the following Document class
236+
237+ .. code-block :: python
238+
239+ class User (Document ):
240+ name = StringField(index = True )
241+
242+ meta = {" indexes" : [" name" ]}
243+
244+ User(name = " John Doe" ).save()
245+
246+ As soon as you start interacting with the Document collection (when `.save() ` is called in this case),
247+ it would create the following indexes:
248+
249+ .. code-block :: python
250+
251+ print (User._get_collection().index_information())
252+ # {
253+ # '_id_': {'key': [('_id', 1)], 'v': 2},
254+ # 'name_1': {'background': False, 'key': [('name', 1)], 'v': 2},
255+ # }
256+
257+ Thus: '_id' which is the default index and 'name_1' which is our custom index.
258+ If you would remove the 'name' field or its index, you would have to call:
259+
260+ .. code-block :: python
261+
262+ User._get_collection().drop_index(' name_1' )
263+
264+ .. note :: When adding new fields or new indexes, MongoEngine will take care of creating them
265+ (unless `auto_create_index ` is disabled) ::
266+
226267Recommendations
227268===============
228269
0 commit comments