elemMatch in timeseries

Hi here
On mongo 6.0.0 and 6.0.1
I have a timeseries with a schema like that

{
   timestamps: number
   metadata: {
      [key: metricName]: { // an array of measurement with different sensor for a same metric
         value: number,
         units: string,
         sensor: {
            name: string,
            height: {
               value: number,
               units: string
            }
         }
      }[]
   }
}

If I try to query

{
  "metadata.MEASURE1": {
    $elemMatch: {
      'sensor.height.value': 0,
      'sensor.height.units': 'm'
    }
  }
}

Doesnt work.
If I do a similar request on non timeseries. Works fine.
Someont met this issues on timeseries ?

Hi @ACHACHE_FRANCOIS and welcome to the MongoDB community!!

Could you please help me with the sample document for the above schema mentioned which would help me to reproduce the problem on the local environment.

In the meantime, I created my own sample documents to check for $elemMatch functionality in a time series collection:
Sample Document

[
  {
    timestamp: ISODate("2021-05-18T00:00:00.000Z"),
    metadata: { sensorId: 5578, type: 'temperature' },
    _id: ObjectId("632aa2743a5ebcfafa682a85"),
    results: [ 1, 2, 3 ],
    temp: 12
  },
  {
    timestamp: ISODate("2021-05-18T00:00:00.000Z"),
    metadata: { sensorId: 5578, type: 'temperature' },
    _id: ObjectId("632aa27e3a5ebcfafa682a86"),
    results: [ 82, 85, 88 ],
    temp: 12
  },
  {
    timestamp: ISODate("2022-09-20T10:54:17.944Z"),
    results: [ 82, 85, 88 ],
    _id: ObjectId("63299bd941f051910f8be559")
  },
  {
    timestamp: ISODate("2022-09-20T10:54:26.328Z"),
    results: [ 1, 2, 3 ],
    _id: ObjectId("63299be241f051910f8be55a")
  }
]

Query using $elemMatch

db.test.find( { results: { $elemMatch: { $gte: 80, $lt: 85 } } })
[
  {
    timestamp: ISODate("2021-05-18T00:00:00.000Z"),
    metadata: { sensorId: 5578, type: 'temperature' },
    _id: ObjectId("632aa27e3a5ebcfafa682a86"),
    results: [ 82, 85, 88 ],
    temp: 12
  },
  {
    timestamp: ISODate("2022-09-20T10:54:17.944Z"),
    results: [ 82, 85, 88 ],
    _id: ObjectId("63299bd941f051910f8be559")
  }
]

and it works as expected in the MongoDB version 6.0.

Best Regards
Aasawari

1 Like