I want to add a field to the sort
clause, for only those documents that created one day ago.
Suppose in my collection mycollection
my documents have a field publishDate
, and some other fields, and following is my query:
db.getCollection('mycollection').aggregate([
{
"$match": {
"expireDate": {
"$gte": ISODate("2019-03-14T00:00:00.000Z")
},
"publishDate": {
"$lt": ISODate("2019-03-15T00:00:00.000Z")
},
"isPublished": true,
"isDrafted": false,
"deletedAt": {
"$eq": null
},
"deleted": false,
"blocked": {
"$exists": false
}
}
},
{
"$sort": {
"isFeatured": -1, // What I want is to add this field in sort clause by condition, when publishDate is yesterday, otherwise ignore it
"refreshes.refreshAt": -1,
"publishDate": -1,
"_id": -1
}
},
{
"$skip": 0
},
{
"$limit": 12
},
{
"$project": {
"position": 1
}
}])