How to return a range of readings (or latest) from all sensors of a type from a time-series collection

Hello everyone,

I want to use a time-series collection (or any collection for that matter) with the following document schema:

{
    "metadata": {
        "sensorId": "123B",
        "type": "A"
    },
    "timestamp": ISODate(...),
    "reading": 7.0
}

The idea is to return the latest reading (or multiple values within a time range) for all sensors of a particular type.

For example, I insert the following:

[
    {
        "metadata": {
            "sensorId": "10",
            "type": "A"
        },
        "timestamp": 1,
        "reading": 1.0
    },
    {
        "metadata": {
            "sensorId": "10",
            "type": "A"
        },
        "timestamp": 1,
        "reading": 2.0
    },
    {
        "metadata": {
            "sensorId": "20",
            "type": "A"
        },
        "timestamp": 2,
        "reading":1.0
    },
    {
        "metadata": {
            "sensorId": "20",
            "type": "A"
        },
        "timestamp": 2,
        "reading":5.0
    },
    {
        "metadata": {
            "sensorId": "20",
            "type": "A"
        },
        "timestamp": 2,
        "reading":9.0
    }
]

I would like to return everything of type “A” with a reading > 1.0. These would be:

[
    {
        "metadata": {
            "sensorId": "10",
            "type": "A"
        },
        "timestamp": 1,
        "reading": 2.0
    },
    {
        "metadata": {
            "sensorId": "20",
            "type": "A"
        },
        "timestamp": 2,
        "reading":5.0
    },
    {
        "metadata": {
            "sensorId": "20",
            "type": "A"
        },
        "timestamp": 2,
        "reading":9.0
    },
]

Similarly, I would like to return everything of type “A” with their latest readings. This would be:

[
    {
        "metadata": {
            "sensorId": "10",
            "type": "A"
        },
        "timestamp": 1,
        "reading": 2.0
    },
    {
        "metadata": {
            "sensorId": "20",
            "type": "A"
        },
        "timestamp": 2,
        "reading":9.0
    },
]

From the documentation it is not clear how one would set up indexing and queries for such a thing. Is it even possible to achieve this in an efficient manner?

I understand time-series does bucketing, so maybe it is not actually the correct collection type to support these queries. Perhaps a normal collection or a clustered collection would work better?

Any help would be greatly appreciated, thank you!

Time-series collections are good to reduce disk usage for all server types, but especially for sharded clusters to distribute data evenly. So, no, other collection types won’t work better.

as for the indexing, it reads (similar to automatic _id indexing):

When you create a time series collection, MongoDB automatically creates an internal clustered index on the time field ( Time Series — MongoDB Manual)

so your question of “range” and “latest” is already part of the answer: you just query your data on timestamp as you normally do for any other field, and the internal index is automatically used meaning it is already efficient.

To efficiently query fields other than timestamp, please make sure you read this page: Add Secondary Indexes to Time Series Collections — MongoDB Manual. It is not much different than making compound indexes.