Du kan använda $facet
aggregering här.
db.test.aggregate([
{ "$facet": {
"first": [
{ "$match": { "category_id": 1 }},
{ "$sort": { "createAt": -1 }},
{ "$limit": 5 }
],
"second": [
{ "$match": { "category_id": 2 }},
{ "$sort": { "createAt": -1 }},
{ "$limit": 5 }
]
}},
{ "$project": { "data": { "$concatArrays": ["$first", "$second"] }}},
{ "$unwind": "$data" },
{ "$replaceRoot": { "newRoot": "$data" }}
])
Uppdatera
Använder enkelt javascript
const test1 = await db.test.find({ category_id: 1 }).sort({ createAt: -1 }).limit(5)
const test2 = await db.test.find({ category_id: 1 }).sort({ createAt: -1 }).limit(5)
const test = test1.concat(test2)
console.log(test)