U kunt $facet
gebruiken
aggregatie hier.
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" }}
])
Bijwerken
Eenvoudig javascript gebruiken
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)