Database

[{title:"AD NUMERO 1",adPromote:"reach-up-ad",expiredAt:ISODate("2023-08-03T16:02:50.000Z"),status:"live"},{title:"AD NUMERO 2",adPromote:"free-ad",expiredAt:ISODate("2023-08-04T16:02:50.000Z"),status:"live"},{title:"AD NUMERO 3",adPromote:"reach-up-ad",expiredAt:ISODate("2023-08-05T16:02:50.000Z"),status:"live"},{title:"AD NUMERO 4",adPromote:"free-ad",expiredAt:ISODate("2023-08-06T16:02:50.000Z"),status:"live"},{title:"AD NUMERO 5",adPromote:"urgent-ad",expiredAt:ISODate("2023-08-07T16:02:50.000Z"),status:"live"},{title:"AD NUMERO 6",adPromote:"free-ad",expiredAt:ISODate("2023-08-08T16:02:50.000Z"),status:"live"},{title:"AD NUMERO 7",adPromote:"reach-up-ad",expiredAt:ISODate("2023-08-09T16:02:50.000Z"),status:"live"},{title:"AD NUMERO 8",adPromote:"reach-up-ad",expiredAt:ISODate("2023-08-10T16:02:50.000Z"),status:"live"}]

Query

db.collection.aggregate([{/**match documents not expired*with "live" status*where adPromote in ['reach-up-ad', 'free-ad'] to remove 'urgent-ad'*/$match:{$and:[{expiredAt:{$gte:ISODate("2023-08-04T12:02:50.000Z")}},{"status":"live"},{adPromote:{$in:["reach-up-ad","free-ad"]}}]}},{$addFields:{/** add a "prioritySort" field to allow custom sort according the value of "adPromote" field*/prioritySort:{$switch:{branches:[{case:{$eq:["$adPromote","reach-up-ad"]},then:3},{case:{$eq:["$adPromote","free-ad"]},then:2},{case:{$eq:["$adPromote","urgent-ad"]},then:1}],default:0}},/**add other custom field here*coolTitle: {$concat: ["$title"," is a cool title"]}*/}},/** sort by prioritySort field added* sort then by _id to keep same order and have consistent result*/{$sort:{prioritySort:-1,_id:1}},/** skip 1 value*/{$skip:1},/** take the 3 next*/{$limit:3},/**optional remove added fields with unset*/{$unset:["prioritySort",/** "coolTitle"*/]}])

Result