How to $slice a $filter result in MongoDB?

Viewed 2501

I have a collection with the following format:

{
    "_id": 123,
    "items": [{
        "status" : "inactive",
        "created" : ISODate("2016-03-16T10:39:28.321Z")
    },
    {
        "status" : "active",
        "created" : ISODate("2016-03-16T10:39:28.321Z")
    },
    {
        "status" : "active",
        "created" : ISODate("2016-03-16T10:39:28.321Z")
    }
    ],
    "status" : "active"
}

I want to query on status field of items such that the object with status as 'active' is only returned in the array and also only the last 2 are returned in the query.

At present I am using $filter for this operation, but I am not able to use $slice along with $filter(which I think is needed for what I desire). The following is how my query looks right now:

db.collection('collection').aggregate([
{$match: {'status': 'active'}},
{
    $project: {
        'items': {
            $filter: {
                input: '$items', 
                as: 'item', 
                cond: {$eq: ['$$item.status', 'active']
            }
        }
    }
}]);

What I am getting right now is correct result, its just that it returns all the objects in the items field and I just want the last 2 objects.

1 Answers
Related