'Checks the document meta data and ensures all the indexes exist.
Global defaults can be set in the meta - see :doc:`guide/defining-documents`
.. note:: You can disable automatic index creation by setting
`auto_create_index` to False in the documents meta data'

@classmethod
def ensure_indexes(cls):

    background = cls._meta.get('index_background', False)
    drop_dups = cls._meta.get('index_drop_dups', False)
    index_opts = (cls._meta.get('index_opts') or {})
    index_cls = cls._meta.get('index_cls', True)
    if (IS_PYMONGO_3 and drop_dups):
        msg = 'drop_dups    is    deprecated    and    is    removed    when    using    PyMongo    3+.'
        warnings.warn(msg, DeprecationWarning)
    collection = cls._get_collection()
    if ((not collection.is_mongos) and (collection.read_preference > 1)):
        return
    cls_indexed = False
    if cls._meta['index_specs']:
        index_spec = cls._meta['index_specs']
        for spec in index_spec:
            spec = spec.copy()
            fields = spec.pop('fields')
            cls_indexed = (cls_indexed or includes_cls(fields))
            opts = index_opts.copy()
            opts.update(spec)
            if ('cls' in opts):
                del opts['cls']
            if IS_PYMONGO_3:
                collection.create_index(fields, background=background, **opts)
            else:
                collection.ensure_index(fields, background=background, drop_dups=drop_dups, **opts)
    if (index_cls and (not cls_indexed) and cls._meta.get('allow_inheritance')):
        if ('cls' in index_opts):
            del index_opts['cls']
        if IS_PYMONGO_3:
            collection.create_index('_cls', background=background, **index_opts)
        else:
            collection.ensure_index('_cls', background=background, **index_opts)
