I have a document like this:
{
_id:'5e2b8a2065318f95166deedc'
expenses:[{amount:100},{amount:200}]
},
{
_id:'5e2b8a2065318f95166deedc'
expenses:[]
},
{
_id:'5e2b8a2065318f95166deedc'
expenses:[{amount:400},{amount:600}]
}
I need to query with aggregate to return the fields WITH EXPENSES, that is expenses with empty array should not be returned to the next step of the pipeline
This is what I have tried so far:
Exps.aggregate([
{$match: {"id":ObjectId(myId)}},
{$group:{
_id:'$_id',
expenses:{$last:"$expenses"},
}}
])
But this returns all three subdocuments including the empty one. How can I just get the 1st and 3rd subdocuments (as per my example) so that i can pass them to the next step on the pipeline?