File: time_series.md

package info (click to toggle)
python-beanie 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,480 kB
  • sloc: python: 14,427; makefile: 7; sh: 6
file content (31 lines) | stat: -rw-r--r-- 1,057 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Time series

You can set up a timeseries collection using the inner `Settings` class.

**Be aware, timeseries collections a supported by MongoDB 5.0 and higher only. The fields `bucket_max_span_seconds` and `bucket_rounding_seconds` however require MongoDB 6.3 or higher**

```python
from datetime import datetime

from beanie import Document, TimeSeriesConfig, Granularity
from pydantic import Field


class Sample(Document):
    ts: datetime = Field(default_factory=datetime.now)
    meta: str

    class Settings:
        timeseries = TimeSeriesConfig(
            time_field="ts", #  Required
            meta_field="meta", #  Optional
            granularity=Granularity.hours, #  Optional
            bucket_max_span_seconds=3600,  #  Optional
            bucket_rounding_seconds=3600,  #  Optional
            expire_after_seconds=2  #  Optional
        )
```

TimeSeriesConfig fields reflect the respective parameters of the MongoDB timeseries creation function.

MongoDB documentation: https://docs.mongodb.com/manual/core/timeseries-collections/