def create_indexes(self, indexes, session=None, **kwargs):
        """Create one or more indexes on this collection.

          >>> from pymongo import IndexModel, ASCENDING, DESCENDING
          >>> index1 = IndexModel([("hello", DESCENDING),
          ...                      ("world", ASCENDING)], name="hello_world")
          >>> index2 = IndexModel([("goodbye", DESCENDING)])
          >>> db.test.create_indexes([index1, index2])
          ["hello_world", "goodbye_-1"]

        :Parameters:
          - `indexes`: A list of :class:`~pymongo.operations.IndexModel`
            instances.
          - `session` (optional): a
            :class:`~pymongo.client_session.ClientSession`.
          - `**kwargs` (optional): optional arguments to the createIndexes
            command (like maxTimeMS) can be passed as keyword arguments.

        .. note:: `create_indexes` uses the `createIndexes`_ command
           introduced in MongoDB **2.6** and cannot be used with earlier
           versions.

        .. note:: The :attr:`~pymongo.collection.Collection.write_concern` of
           this collection is automatically applied to this operation when using
           MongoDB >= 3.4.

        .. versionchanged:: 3.6
           Added ``session`` parameter. Added support for arbitrary keyword
           arguments.

        .. versionchanged:: 3.4
           Apply this collection's write concern automatically to this operation
           when connected to MongoDB >= 3.4.
        .. versionadded:: 3.0

        .. _createIndexes: https://docs.mongodb.com/manual/reference/command/createIndexes/
        """
        common.validate_list('indexes', indexes)
        names = []
        with self._socket_for_writes(session) as sock_info:
            supports_collations = sock_info.max_wire_version >= 5
            def gen_indexes():
                for index in indexes:
                    if not isinstance(index, IndexModel):
                        raise TypeError(
                            "%r is not an instance of "
                            "pymongo.operations.IndexModel" % (index,))
                    document = index.document
                    if "collation" in document and not supports_collations:
                        raise ConfigurationError(
                            "Must be connected to MongoDB "
                            "3.4+ to use collations.")
                    names.append(document["name"])
                    yield document
            cmd = SON([('createIndexes', self.name),
                       ('indexes', list(gen_indexes()))])
            cmd.update(kwargs)
            self._command(
                sock_info, cmd, read_preference=ReadPreference.PRIMARY,
                codec_options=_UNICODE_REPLACE_CODEC_OPTIONS,
                write_concern=self._write_concern_for(session),
                session=session)
        return names
def create_indexes ( self , indexes , session = None , ** kwargs ) : common . validate_list ( 'indexes' , indexes ) names = [ ] with self . _socket_for_writes ( session ) as sock_info : supports_collations = sock_info . max_wire_version >= 5 def gen_indexes ( ) : for index in indexes : if not isinstance ( index , IndexModel ) : raise TypeError ( "%r is not an instance of " "pymongo.operations.IndexModel" % ( index , ) ) document = index . document if "collation" in document and not supports_collations : raise ConfigurationError ( "Must be connected to MongoDB " "3.4+ to use collations." ) names . append ( document [ "name" ] ) yield document cmd = SON ( [ ( 'createIndexes' , self . name ) , ( 'indexes' , list ( gen_indexes ( ) ) ) ] ) cmd . update ( kwargs ) self . _command ( sock_info , cmd , read_preference = ReadPreference . PRIMARY , codec_options = _UNICODE_REPLACE_CODEC_OPTIONS , write_concern = self . _write_concern_for ( session ) , session = session ) return names
Create one or more indexes on this collection.

          >>> from pymongo import IndexModel, ASCENDING, DESCENDING
          >>> index1 = IndexModel([("hello", DESCENDING),
          ...                      ("world", ASCENDING)], name="hello_world")
          >>> index2 = IndexModel([("goodbye", DESCENDING)])
          >>> db.test.create_indexes([index1, index2])
          ["hello_world", "goodbye_-1"]

        :Parameters:
          - `indexes`: A list of :class:`~pymongo.operations.IndexModel`
            instances.
          - `session` (optional): a
            :class:`~pymongo.client_session.ClientSession`.
          - `**kwargs` (optional): optional arguments to the createIndexes
            command (like maxTimeMS) can be passed as keyword arguments.

        .. note:: `create_indexes` uses the `createIndexes`_ command
           introduced in MongoDB **2.6** and cannot be used with earlier
           versions.

        .. note:: The :attr:`~pymongo.collection.Collection.write_concern` of
           this collection is automatically applied to this operation when using
           MongoDB >= 3.4.

        .. versionchanged:: 3.6
           Added ``session`` parameter. Added support for arbitrary keyword
           arguments.

        .. versionchanged:: 3.4
           Apply this collection's write concern automatically to this operation
           when connected to MongoDB >= 3.4.
        .. versionadded:: 3.0

        .. _createIndexes: https://docs.mongodb.com/manual/reference/command/createIndexes/