How does MongoEngine handle Indexes (creation, update, removal)?

女生的网名这么多〃 提交于 2019-12-05 08:25:51

You can add an index at any time and ensureIndex will be called behind the scenes so it will be added if it doesn't exist.

If you remove an index from the meta - you will have to use pymongo or the shell to remove the index.

VISHAL KUMAWAT

MongoEngine provides a programming construct to maintain all indexes from your python application.It uses metadata on collection class to define all your indexes. Here is an example

class User(Document):
    meta = {        
    'indexes': [
    {
         'fields': ['+name']                   
    },
    {
         'fields': ['#email']
    }]             
}

The User class defined above declares two indexes: 1. name (sort order) and 2. email (hashed). MongoEngine creates each declared index at the first upsert operation. These indexes are created on the collection via a createIndex/ensureIndex call . MongoEngine attempts to create these indexes every time a document is inserted into the collection.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!