improve list_indexes, ensure_indexes docstring and add index migration example

This commit is contained in:
Bastien Gerard 2021-08-22 22:01:05 +02:00
parent 533700583d
commit 1b249e336e
3 changed files with 51 additions and 4 deletions

View File

@ -628,8 +628,8 @@ point. To create a geospatial index you must prefix the field with the
], ],
} }
Time To Live indexes Time To Live (TTL) indexes
-------------------- --------------------------
A special index type that allows you to automatically expire data from a A special index type that allows you to automatically expire data from a
collection after a given period. See the official collection after a given period. See the official

View File

@ -223,6 +223,47 @@ it is often useful for complex migrations of Document models.
.. warning:: Be aware of this `flaw <https://groups.google.com/g/mongodb-user/c/AFC1ia7MHzk>`_ if you modify documents while iterating .. warning:: Be aware of this `flaw <https://groups.google.com/g/mongodb-user/c/AFC1ia7MHzk>`_ if you modify documents while iterating
Example 4: Index removal
========================
If you remove an index from your Document class, or remove an indexed Field from your Document class,
you'll need to manually drop the corresponding index. MongoEngine will not do that for you.
The way to deal with this case is to identify the name of the index to drop with `index_information()`, and then drop
it with `drop_index()`
Let's for instance assume that you start with the following Document class
.. code-block:: python
class User(Document):
name = StringField(index=True)
meta = {"indexes": ["name"]}
User(name="John Doe").save()
As soon as you start interacting with the Document collection (when `.save()` is called in this case),
it would create the following indexes:
.. code-block:: python
print(User._get_collection().index_information())
# {
# '_id_': {'key': [('_id', 1)], 'v': 2},
# 'name_1': {'background': False, 'key': [('name', 1)], 'v': 2},
# }
Thus: '_id' which is the default index and 'name_1' which is our custom index.
If you would remove the 'name' field or its index, you would have to call:
.. code-block:: python
User._get_collection().drop_index('name_1')
.. note:: When adding new fields or new indexes, MongoEngine will take care of creating them
(unless `auto_create_index` is disabled) ::
Recommendations Recommendations
=============== ===============

View File

@ -875,6 +875,10 @@ class Document(BaseDocument, metaclass=TopLevelDocumentMetaclass):
Global defaults can be set in the meta - see :doc:`guide/defining-documents` Global defaults can be set in the meta - see :doc:`guide/defining-documents`
By default, this will get called automatically upon first interaction with the
Document collection (query, save, etc) so unless you disabled `auto_create_index`, you
shouldn't have to call this manually.
.. note:: You can disable automatic index creation by setting .. note:: You can disable automatic index creation by setting
`auto_create_index` to False in the documents meta data `auto_create_index` to False in the documents meta data
""" """
@ -924,8 +928,10 @@ class Document(BaseDocument, metaclass=TopLevelDocumentMetaclass):
@classmethod @classmethod
def list_indexes(cls): def list_indexes(cls):
"""Lists all of the indexes that should be created for given """Lists all indexes that should be created for the Document collection.
collection. It includes all the indexes from super- and sub-classes. It includes all the indexes from super- and sub-classes.
Note that it will only return the indexes' fields, not the indexes' options
""" """
if cls._meta.get("abstract"): if cls._meta.get("abstract"):
return [] return []