Creates an index on this collection. Takes either a single key or a list containing (key, direction) pairs or keys. If no direction is given, :data:`~pymongo.ASCENDING` will be assumed. The key(s) must be an instance of :class:`str` and the direction(s) must
(
self,
keys: _IndexKeyHint,
session: Optional[ClientSession] = None,
comment: Optional[Any] = None,
**kwargs: Any,
)
| 2273 | return self.database.client._retryable_write(False, inner, session, _Op.CREATE_INDEXES) |
| 2274 | |
| 2275 | def create_index( |
| 2276 | self, |
| 2277 | keys: _IndexKeyHint, |
| 2278 | session: Optional[ClientSession] = None, |
| 2279 | comment: Optional[Any] = None, |
| 2280 | **kwargs: Any, |
| 2281 | ) -> str: |
| 2282 | """Creates an index on this collection. |
| 2283 | |
| 2284 | Takes either a single key or a list containing (key, direction) pairs |
| 2285 | or keys. If no direction is given, :data:`~pymongo.ASCENDING` will |
| 2286 | be assumed. |
| 2287 | The key(s) must be an instance of :class:`str` and the direction(s) must |
| 2288 | be one of (:data:`~pymongo.ASCENDING`, :data:`~pymongo.DESCENDING`, |
| 2289 | :data:`~pymongo.GEO2D`, :data:`~pymongo.GEOSPHERE`, |
| 2290 | :data:`~pymongo.HASHED`, :data:`~pymongo.TEXT`). |
| 2291 | |
| 2292 | To create a single key ascending index on the key ``'mike'`` we just |
| 2293 | use a string argument:: |
| 2294 | |
| 2295 | >>> my_collection.create_index("mike") |
| 2296 | |
| 2297 | For a compound index on ``'mike'`` descending and ``'eliot'`` |
| 2298 | ascending we need to use a list of tuples:: |
| 2299 | |
| 2300 | >>> my_collection.create_index([("mike", pymongo.DESCENDING), |
| 2301 | ... "eliot"]) |
| 2302 | |
| 2303 | All optional index creation parameters should be passed as |
| 2304 | keyword arguments to this method. For example:: |
| 2305 | |
| 2306 | >>> my_collection.create_index([("mike", pymongo.DESCENDING)], |
| 2307 | ... background=True) |
| 2308 | |
| 2309 | Valid options include, but are not limited to: |
| 2310 | |
| 2311 | - `name`: custom name to use for this index - if none is |
| 2312 | given, a name will be generated. |
| 2313 | - `unique`: if ``True``, creates a uniqueness constraint on the |
| 2314 | index. |
| 2315 | - `background`: if ``True``, this index should be created in the |
| 2316 | background. |
| 2317 | - `sparse`: if ``True``, omit from the index any documents that lack |
| 2318 | the indexed field. |
| 2319 | - `bucketSize`: for use with geoHaystack indexes. |
| 2320 | Number of documents to group together within a certain proximity |
| 2321 | to a given longitude and latitude. |
| 2322 | - `min`: minimum value for keys in a :data:`~pymongo.GEO2D` |
| 2323 | index. |
| 2324 | - `max`: maximum value for keys in a :data:`~pymongo.GEO2D` |
| 2325 | index. |
| 2326 | - `expireAfterSeconds`: <int> Used to create an expiring (TTL) |
| 2327 | collection. MongoDB will automatically delete documents from |
| 2328 | this collection after <int> seconds. The indexed field must |
| 2329 | be a UTC datetime or the data will not expire. |
| 2330 | - `partialFilterExpression`: A document that specifies a filter for |
| 2331 | a partial index. |
| 2332 | - `collation` (optional): An instance of |