Data Structure:
{
_id: '1',
'title: 'Blabla',
comments: [ {id:'3', type:'normal'} ,
{id: '4', type: 'admin'},
{id: '5', type: 'admin'},
{id: '6', type: 'admin'}
]
}
What i am trying to get is an array of last 5 comments of type admin:
{ comments: [{id:4, type:'admin'}, {id: '5', type: 'admin'}, {id: '6', type: 'admin'}] }
What i have tried:
db.collection('posts').find({_id: '1', 'comments.type':'admin'}
, {
comments:{$slice: -5},
comments:1, _id:0
})
But it returns the hole comments array if at least 1 item with type admin exists.