def index(db, collection, fields, directions=None, desc=False, background=False):
import pymongo<EOL>if type(fields) in STR_TYPES:<EOL><INDENT>fields = [fields, ]<EOL><DEDENT>if directions is None:<EOL><INDENT>_dir = pymongo.DESCENDING if desc else pymongo.ASCENDING<EOL>directions = [_dir] * len(fields)<EOL><DEDENT>field_tuples = list(zip(fields, directions))<EOL>coll = db[collection]<EOL>coll.create_index(field_tuples, background=background)<EOL>
Builds a simple (single field) or complex (multiple fields) index
on a single collection in a MongoDB database.

Args:

    db (Database): A pymongo Database object.

    collection (str): Collection name.

    fields: Can be one of two things:

        - the name of a single field, as a string
        - an iterable (list/tuple) of one or more field names

    desc (bool): If ``True``, all indexes will be created in descending order.
        Default is ``False``.

    directions (list): For complex indexes for which you'd like to have
        different indexing directions (ascending for some fields, descending
        for others), you can pass a list of pymongo direction objects (
        pymongo.ASCENDING and pymongo.DESCENDING), in the same order as the
        list of fields to be indexed. Must be the same length as the list
        of index fields. Default is ``None``.

    background (bool): If ``True``, the indexing operation will be processed
        in the background. When performing background indexes, the MongoDB
        database will not be locked.