I have my document structure in this way. Where i am storing all the events based occured with timestamps. My document look like this
[
{
"_id": {
"$oid": "589341cff92be305c034cb5a"
},
"__v": 0,
"name": "TV",
"switch_event": [
{
"timestamp": 1486186277826,
"event_type": "on"
},
{
"timestamp": 1486272677826,
"event_type": "off"
},
{
"timestamp": 1486099877826,
"event_type": "off"
},
{
"timestamp": 1486186277826,
"event_type": "on"
},
{
"timestamp": 1486272677826,
"event_type": "off"
},
{
"timestamp": 1486099877826,
"event_type": "off"
}
]
}
]
Now while querying for this document i am interested in only the events which occured today. So after querying for this i am writing projection query like this (for testing i kept timestamp > 0, which should give all events) -
SwitchAppliance.find({_id:"589341cff92be305c034cb5a"},{
name:1,
switch_event:{$elemMatch: {
timestamp: {
$gt:0
}
}}
},(err,results)=>{
if(err) {console.log(err);return next({message: "Internal Server Error"});}
return res.json(results);
} );
But when i am getting result i am only getting one event object in the switch_event array- like this -
[
{
"_id": "589341cff92be305c034cb5a",
"switch_event": [
{
"_id": "589567251c653a0890b8b1ef",
"event_type": "on",
"timestamp": 1486186277826
}
],
"name": "TV"
}
]