Skip to content

Commit

Permalink
Merge pull request #2561 from bagerard/improve_index_doc
Browse files Browse the repository at this point in the history
improve index doc
  • Loading branch information
erdenezul committed Aug 23, 2021
2 parents 5337005 + 1b249e3 commit 5fe9436
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
4 changes: 2 additions & 2 deletions docs/guide/defining-documents.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
collection after a given period. See the official
Expand Down
41 changes: 41 additions & 0 deletions docs/guide/migration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

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
===============

Expand Down
10 changes: 8 additions & 2 deletions mongoengine/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,10 @@ def ensure_indexes(cls):
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
`auto_create_index` to False in the documents meta data
"""
Expand Down Expand Up @@ -924,8 +928,10 @@ def ensure_indexes(cls):

@classmethod
def list_indexes(cls):
"""Lists all of the indexes that should be created for given
collection. It includes all the indexes from super- and sub-classes.
"""Lists all indexes that should be created for the Document collection.
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"):
return []
Expand Down

0 comments on commit 5fe9436

Please sign in to comment.