Can I make a collection of collections?

Hey everyone, big MongoDB fan here. :grinning:

Using Beanie object-mapper for Python.

A lot of my collections are time series, therefore I use the time series settings when creating a collection:

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

    class Settings:
        timeseries = TimeSeriesConfig(
            time_field="ts", #  Require
        )

However, I would like to create a collection of these time series collection. Can I do this? If so, what is the easiest way? The example I give below assumes the “timeseries_a” field to be a single document when I try this way.

Example:

class CollectionOfTimeSeries(Document):
    timeseries_a: SampleTimeSeries
    timeseries_b: SampleTimeSeries

“Time Series Collection” is just another collection, but

  • must be created as a “timeseries” from start
  • specifically designed to hold a single “timestamp” at top level
  • internally indexed by this timestamp
  • can hold a metadata
  • can hold other data fields.

you are trying to embed them into another collection. then they are no more time series collections. they will become just documents holding some time data.

but you can create a new time series collection, and use their timestamp fields and insert into this new one if they share exact timestamps, or make adjustments to their timestamps then insert.

{
timestamp: ISODate("2021-05-18T00:00:00.000Z",
tempdata:{
      "timestamp": ISODate("2021-05-18T00:00:00.000Z"),
      "temp": 12
   },
humiditydata:{
      "timestamp": ISODate("2021-05-18T00:00:00.000Z"),
      "humidity": 50
   }
}

remember your data does not have to have all fields in mind in mongodb. so you can insert 2 different-field documents at any time, but you need that top level timestamp if you want to make use of good things about time series collections.

if this is not what you have in mind, then you may try making “views” of your data by $lookup and $facet aggregations to combine the two time series collections, but the result won’t be a time series unless you insert the result into a new one with the above structure.

1 Like

Thanks Yilmaz.

In case anyone sees this post in the future, I decided to combine both time series into a single time series. Like Yilmaz said, the data does not have to have all fields in mind. Therefore, I combined the two time series into a single time series with each document holding either the A values or the B values.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.