I have a document like this..
{
"_id" : ObjectId("59b0ea9b3a91af574a3e0464"),
"machineID" : "b83c",
"sensorState" : [
{
"data" : "377",
"sensor" : "solar",
"time" : ISODate("2017-09-20T19:42:58.766Z")
},
{
"data" : "35",
"sensor" : "photosynthetic",
"time" : ISODate("2017-09-20T19:42:58.782Z")
},
{
"data" : "370",
"sensor" : "solar",
"time" : ISODate("2017-09-20T19:43:29.089Z")
},
{
"data" : "400",
"sensor" : "solar",
"time" : ISODate("2017-09-20T19:44:29.089Z")
},
{
"data" : "35",
"sensor" : "photosynthetic",
"time" : ISODate("2017-09-20T19:43:29.110Z")
}
]
}
I want to retrieve only those sub documents in the sensorState array which match sensor=solar. So, I tried to do like this:
db.getCollection('sensorDB').find({ },
{ "sensorState" : { $elemMatch: {data : "35" } } })
But it shows only one result:
{
"_id" : ObjectId("59b0ea9b3a91af574a3e0464"),
"sensorState" : [
{
"data" : "35",
"sensor" : "photosynthetic",
"time" : ISODate("2017-09-20T19:42:58.782Z")
}
]
}
However, I want to find all sub documents in the sensorState array which match sensor=solar. It looks like $elemMatch just can select one result.