diff options
Diffstat (limited to 'mongoengine/document.py')
| -rw-r--r-- | mongoengine/document.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/mongoengine/document.py b/mongoengine/document.py index 3fddec3..ad166c9 100644 --- a/mongoengine/document.py +++ b/mongoengine/document.py @@ -220,6 +220,8 @@ class Document(BaseDocument, metaclass=TopLevelDocumentMetaclass): # Get the collection, either capped or regular. if cls._meta.get("max_size") or cls._meta.get("max_documents"): cls._collection = cls._get_capped_collection() + elif cls._meta.get("timeseries"): + cls._collection = cls._get_timeseries_collection() else: db = cls._get_db() collection_name = cls._get_collection_name() @@ -271,6 +273,27 @@ class Document(BaseDocument, metaclass=TopLevelDocumentMetaclass): return db.create_collection(collection_name, **opts) + @classmethod + def _get_timeseries_collection(cls): + """Create a new or get an existing timeseries PyMongo collection.""" + db = cls._get_db() + collection_name = cls._get_collection_name() + timeseries_opts = cls._meta.get("timeseries") + + if collection_name in list_collection_names( + db, include_system_collections=True + ): + collection = db[collection_name] + collection.options() + return collection + + opts = {"expireAfterSeconds": timeseries_opts.pop("expireAfterSeconds", None)} + return db.create_collection( + name=collection_name, + timeseries=timeseries_opts, + **opts, + ) + def to_mongo(self, *args, **kwargs): data = super().to_mongo(*args, **kwargs) |
