Help Making Views

Hello Everyone! I’m hoping this is an easy question but it’s proving difficult for me and I’m definitely not understanding some things.

I have two collections:

  1. telemetry - a time series collection with documents that have the format

    { metadata: { device_id: AABBCCDD }, ts: datetime, value: 0.1234}

  2. logs - a regular collection with documents that have the format

    { device_id: AABBCCDD, mesage: “a log message”, ts: datetime}

I would like to create a view called “status” that has three fields (device_id, last_telemetry_received and last_log_received) that updates whenever a log message or telemetry value is received. The idea is that instead of querying each collection individually to display the last timestamp value, I can query the status view.

My problems are two fold:

I don’t know how to create a view in Python and I don’t know how to create a pipeline that will take new data as it comes in to either collection and updates the view. I’m also having a heck of a time finding relevant documentation on merging multiple collections. Any links would be appreciated.


Hi @Neale_Petrillo!

To combine the data of two collections, you can use an aggregation pipeline with a $lookup stage. Just be mindful that $lookup is a costly stage, and to work with a good performance, it’s recommended to have indexes in the fields used to combine the collections.

Regarding views, they are slightly different in MongoDB compared to relational databases. In MongoDB, a view is just a collection followed by an aggregation pipeline. You can learn more in the official documentation.