Skip to content

Commit 5fe9436

Browse files
author
erdenezul
authored
Merge pull request MongoEngine#2561 from bagerard/improve_index_doc
improve index doc
2 parents 5337005 + 1b249e3 commit 5fe9436

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

docs/guide/defining-documents.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,8 @@ point. To create a geospatial index you must prefix the field with the
628628
],
629629
}
630630

631-
Time To Live indexes
632-
--------------------
631+
Time To Live (TTL) indexes
632+
--------------------------
633633

634634
A special index type that allows you to automatically expire data from a
635635
collection after a given period. See the official

docs/guide/migration.rst

+41
Original file line numberDiff line numberDiff line change
@@ -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+
226267
Recommendations
227268
===============
228269

mongoengine/document.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,10 @@ def ensure_indexes(cls):
875875
876876
Global defaults can be set in the meta - see :doc:`guide/defining-documents`
877877
878+
By default, this will get called automatically upon first interaction with the
879+
Document collection (query, save, etc) so unless you disabled `auto_create_index`, you
880+
shouldn't have to call this manually.
881+
878882
.. note:: You can disable automatic index creation by setting
879883
`auto_create_index` to False in the documents meta data
880884
"""
@@ -924,8 +928,10 @@ def ensure_indexes(cls):
924928

925929
@classmethod
926930
def list_indexes(cls):
927-
"""Lists all of the indexes that should be created for given
928-
collection. It includes all the indexes from super- and sub-classes.
931+
"""Lists all indexes that should be created for the Document collection.
932+
It includes all the indexes from super- and sub-classes.
933+
934+
Note that it will only return the indexes' fields, not the indexes' options
929935
"""
930936
if cls._meta.get("abstract"):
931937
return []

0 commit comments

Comments
 (0)