Internal createIndexes helper. :param indexes: A list of :class:`~pymongo.operations.IndexModel` instances. :param session: a :class:`~pymongo.client_session.ClientSession`. :param kwargs: optional arguments to the createIndexes command (l
(
self, indexes: Sequence[IndexModel], session: Optional[ClientSession], **kwargs: Any
)
| 2224 | |
| 2225 | @_csot.apply |
| 2226 | def _create_indexes( |
| 2227 | self, indexes: Sequence[IndexModel], session: Optional[ClientSession], **kwargs: Any |
| 2228 | ) -> list[str]: |
| 2229 | """Internal createIndexes helper. |
| 2230 | |
| 2231 | :param indexes: A list of :class:`~pymongo.operations.IndexModel` |
| 2232 | instances. |
| 2233 | :param session: a |
| 2234 | :class:`~pymongo.client_session.ClientSession`. |
| 2235 | :param kwargs: optional arguments to the createIndexes |
| 2236 | command (like maxTimeMS) can be passed as keyword arguments. |
| 2237 | """ |
| 2238 | names = [] |
| 2239 | |
| 2240 | def inner( |
| 2241 | session: Optional[ClientSession], conn: Connection, _retryable_write: bool |
| 2242 | ) -> list[str]: |
| 2243 | supports_quorum = conn.max_wire_version >= 9 |
| 2244 | |
| 2245 | def gen_indexes() -> Iterator[Mapping[str, Any]]: |
| 2246 | for index in indexes: |
| 2247 | if not isinstance(index, IndexModel): |
| 2248 | raise TypeError( |
| 2249 | f"{index!r} is not an instance of pymongo.operations.IndexModel" |
| 2250 | ) |
| 2251 | document = index.document |
| 2252 | names.append(document["name"]) |
| 2253 | yield document |
| 2254 | |
| 2255 | cmd = {"createIndexes": self.name, "indexes": list(gen_indexes())} |
| 2256 | cmd.update(kwargs) |
| 2257 | if "commitQuorum" in kwargs and not supports_quorum: |
| 2258 | raise ConfigurationError( |
| 2259 | "Must be connected to MongoDB 4.4+ to use the " |
| 2260 | "commitQuorum option for createIndexes" |
| 2261 | ) |
| 2262 | |
| 2263 | self._command( |
| 2264 | conn, |
| 2265 | cmd, |
| 2266 | read_preference=ReadPreference.PRIMARY, |
| 2267 | codec_options=_UNICODE_REPLACE_CODEC_OPTIONS, |
| 2268 | write_concern=self._write_concern_for(session), |
| 2269 | session=session, |
| 2270 | ) |
| 2271 | return names |
| 2272 | |
| 2273 | return self.database.client._retryable_write(False, inner, session, _Op.CREATE_INDEXES) |
| 2274 | |
| 2275 | def create_index( |
| 2276 | self, |
no test coverage detected