I tried the facet as follows:
db.Alerts.aggregate([
{
"$match": {
"status": { "$ne": -1 },
"type": 4
}
},
{
"$sort": {
"status": 1,
"createdAt": -1
}
},
{
$facet: {
result: [
{ $skip: 0 },
{ $limit: 10 },
{
"$lookup": {
"localField": "alertTypeId",
"from": "AlertTypes",
"foreignField": "_id",
"as": "alertTypeRel"
}
},
{
"$project": {
"title": 1,
"type": 1,
"alertTypeId": 1,
"alertTypeRel.alertTypeName": 1,
"priority": 1,
"message": 1,
"status": 1,
"startDate": 1,
"createdAt": 1,
"createdBy": 1,
"validUntil": 1,
"errorFlag": 1,
"extApiId": 1,
"errorMessage": 1,
"autoPublish": 1,
"statusChangedBy": 1
}
}
],
count: [{ $count: "total" }]
}
}
],
{
"collation": {
"locale": "en",
"strength": 2
},
"allowDiskUse": true,
"cursor": {}
})
.pretty();