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!