You can write a query for this but it cannot use an index so it won’t be efficient on a large collection. If you combine it without another query predicate that index could be used though. The way you do it is via $expr which lets you use aggregation expressions.
This returns all documents which match condition that some value of temp inside some field inside data is equal to 30.5 - if you want to get back not the full document but just the subdocument corresponding to that temp value, you also need a projection:
We can just use this query right?
db.collection.find({}, {data:{$arrayToObject:{$filter:{input:{$objectToArray:"$data"},cond:{$eq:["$$this.v.temp",30.5]}}}}}
)
Well, having {} as the query predicate means you will examine and returnevery single document in the collection and then end up returning empty data object for documents that didn’t have any elements which had the temperature you’re interested in.
Ah okay. When I tried it with the query predicate on my node.js environment, I get the following error:
UnhandledPromiseRejectionWarning: MongoError: empty variable names are not allowed