0

I have a db with documents representing time-series measurements.

'valuesMin' can be in range 0->59 but there is no guarantee that all 60 values will be present.

{
"timeStampHour" : ISODate("2019-02-28T12:00:00.000Z"),
"valuesMin" : {
    "0" : 20,
    "1" : 20,
    "2" : 22,
    "3" : 23,
    ....
    "54" : 32
}
}
{
"timeStampHour" : ISODate("2019-02-28T13:00:00.000Z"),
"valuesMin" : {
    "5" : 20,
    "10" : 25,
    "14" : 27,
    "15" : 30,
    ....
    "59" : 40
}
}

How do I go about searching documents where any "valuesMin" field contains a value $gte 35?

Something along the lines of:

db.getCollection('sensorData').find({
    'timeStampHour':{
        $elemMatch:{'valuesMin.*':{$gte: 35}}
    }
})

My search seems to show that true "*" wildcard is not doable, but is there a work around if I know that the keys are always between '0' and '59'?

Borisw37
  • 739
  • 2
  • 7
  • 30
  • 1
    check here https://stackoverflow.com/questions/19802502/mongodb-query-help-query-on-values-of-any-key-in-a-sub-object – molamk Mar 02 '19 at 02:01

1 Answers1

0

This can be achived using $objectToArray and $arrayToObject

db.q3.aggregate([
    {$project: {"valuesMin": {$objectToArray: "$valuesMin"}}},
    {$match:{"valuesMin.v": {$gte: 35}}},
    {$project: {"valuesMin": {$arrayToObject: "$valuesMin"}}}
])
Mani
  • 1,471
  • 1
  • 13
  • 19