Return the PyMongo collection corresponding to this document. Upon first call, this method: 1. Initializes a :class:`~pymongo.collection.Collection` corresponding to this document. 2. Creates indexes defined in this document's :attr:`meta` dictionary. T
(cls)
| 208 | |
| 209 | @classmethod |
| 210 | def _get_collection(cls): |
| 211 | """Return the PyMongo collection corresponding to this document. |
| 212 | |
| 213 | Upon first call, this method: |
| 214 | 1. Initializes a :class:`~pymongo.collection.Collection` corresponding |
| 215 | to this document. |
| 216 | 2. Creates indexes defined in this document's :attr:`meta` dictionary. |
| 217 | This happens only if `auto_create_index` is True. |
| 218 | """ |
| 219 | if not hasattr(cls, "_collection") or cls._collection is None: |
| 220 | # Get the collection, either capped or regular. |
| 221 | if cls._meta.get("max_size") or cls._meta.get("max_documents"): |
| 222 | cls._collection = cls._get_capped_collection() |
| 223 | elif cls._meta.get("timeseries"): |
| 224 | cls._collection = cls._get_timeseries_collection() |
| 225 | else: |
| 226 | db = cls._get_db() |
| 227 | collection_name = cls._get_collection_name() |
| 228 | cls._collection = db[collection_name] |
| 229 | |
| 230 | # Ensure indexes on the collection unless auto_create_index was |
| 231 | # set to False. Plus, there is no need to ensure indexes on slave. |
| 232 | db = cls._get_db() |
| 233 | if cls._meta.get("auto_create_index", True) and db.client.is_primary: |
| 234 | cls.ensure_indexes() |
| 235 | |
| 236 | return cls._collection |
| 237 | |
| 238 | @classmethod |
| 239 | def _get_capped_collection(cls): |