Предположим, что ввод:
[
{
"_id": 1,
"status": "pending"
},
{
"_id": 2,
"status": "pending"
},
{
"_id": 3,
"status": "pending"
},
{
"_id": 4,
"status": "pending"
},
{
"_id": 5,
"status": "pending"
},
{
_id: 6,
status: "pending"
},
{
_id: 7,
status: "pending"
},
{
_id: 8,
status: "pending"
},
{
_id: 9,
status: "released"
},
{
_id: 10,
status: "released"
},
{
_id: 11,
status: "released"
},
{
_id: 12,
status: "released"
},
{
_id: 13,
status: "released"
},
{
_id: 14,
status: "released"
},
{
_id: 15,
status: "released"
},
{
_id: 16,
status: "released"
},
]
Вы можете использовать агрегированный конвейер $ facet, чтобы получить 2 массива данных
collectionname.aggregate([
{
$facet: {
pending: [
{
$match: {
status: "pending"
}
},
{
$skip: 0
},
{
$limit: 5
}
],
released: [
{
$match: {
status: "released"
}
},
{
$skip: 0
},
{
$limit: 5
}
]
}
}
])
, давая вывод:
[
{
"pending": [
{
"_id": 1,
"status": "pending"
},
{
"_id": 2,
"status": "pending"
},
{
"_id": 3,
"status": "pending"
},
{
"_id": 4,
"status": "pending"
},
{
"_id": 5,
"status": "pending"
}
],
"released": [
{
"_id": 9,
"status": "released"
},
{
"_id": 10,
"status": "released"
},
{
"_id": 11,
"status": "released"
},
{
"_id": 12,
"status": "released"
},
{
"_id": 13,
"status": "released"
}
]
}
]