Вы можете попробовать ниже:
db.collection.aggregate([
{
$match: {
_id: ObjectId("5ce66416ff87476e5eef0398")
}
},
{
$unwind: "$debtor_plafonds"
},
{
$unwind: "$debtor_plafonds.debtor_taks"
},
{
$unwind: "$debtor_plafonds.debtor_taks.transfers"
},
{
$sort: {
"debtor_plafonds.debtor_taks.transfers.date": -1
}
},
{
$group: {
_id: {
"_id": "$_id",
"tot_plafond": "$tot_plafond",
"debtor_plafonds": "$debtor_plafonds.debtor_id",
"debtor_taks": "$debtor_plafonds.debtor_taks.task_id",
},
// data : { $push: "$$ROOT" },
transfers : {
$push: {
"amount": "$debtor_plafonds.debtor_taks.transfers.amount",
"date": "$debtor_plafonds.debtor_taks.transfers.date"
}
}
}
},
{
$project: {
_id: "$_id._id",
tot_plafond: "$_id.tot_plafond",
debtor_plafonds: "$_id.debtor_plafonds",
debtor_taks: "$_id.debtor_taks",
transfers: { $slice : [ "$transfers",2 ] }
}
}
])
Я попробовал это фиктивные данные. Вы получите результат, как показано ниже:
/* 1 createdAt:23/05/2019, 14:42:54*/
{
"_id" : ObjectId("5ce66416ff87476e5eef0398"),
"tot_plafond" : 20,
"debtor_plafonds" : ObjectId("5c8d5cd4d8d2ab15b37c780a"),
"debtor_taks" : ObjectId("5cad84b93d124a151f633af1"),
"transfers" : [
{
"amount" : 10,
"date" : ISODate("2019-02-18T10:32:00.313+05:30")
},
{
"amount" : 10,
"date" : ISODate("2019-02-18T10:32:00.313+05:30")
}
]
},
/* 2 createdAt:23/05/2019, 14:42:54*/
{
"_id" : ObjectId("5ce66416ff87476e5eef0398"),
"tot_plafond" : 20,
"debtor_plafonds" : ObjectId("5c8d5cd4d8d2ab15b37c780a"),
"debtor_taks" : ObjectId("5c91c0f121a78f19d9eb3531"),
"transfers" : [
{
"amount" : 10,
"date" : ISODate("2019-02-18T10:32:00.313+05:30")
},
{
"amount" : 10,
"date" : ISODate("2019-02-18T10:32:00.313+05:30")
}
]
},
/* 3 createdAt:23/05/2019, 14:42:54*/
{
"_id" : ObjectId("5ce66416ff87476e5eef0398"),
"tot_plafond" : 20,
"debtor_plafonds" : ObjectId("5ca449f6b3171315a29a90dc"),
"debtor_taks" : ObjectId("5ca449f6b3171315a29a90db"),
"transfers" : [
{
"amount" : 10,
"date" : ISODate("2019-02-20T10:32:00.313+05:30")
},
{
"amount" : 10,
"date" : ISODate("2019-02-19T10:32:00.313+05:30")
}
]
},
/* 4 createdAt:23/05/2019, 14:42:54*/
{
"_id" : ObjectId("5ce66416ff87476e5eef0398"),
"tot_plafond" : 20,
"debtor_plafonds" : ObjectId("5cad84b93d124a151f633af0"),
"debtor_taks" : ObjectId("5cad84b93d124a151f633af2"),
"transfers" : [
{
"amount" : 10,
"date" : ISODate("2019-02-18T10:32:00.313+05:30")
},
{
"amount" : 10,
"date" : ISODate("2019-02-18T10:32:00.313+05:30")
}
]
},
/* 5 createdAt:23/05/2019, 14:42:54*/
{
"_id" : ObjectId("5ce66416ff87476e5eef0398"),
"tot_plafond" : 20,
"debtor_plafonds" : ObjectId("5ca449f6b3171315a29a90dc"),
"debtor_taks" : ObjectId("5cad84b93d124a151f633af3"),
"transfers" : [
{
"amount" : 10,
"date" : ISODate("2019-02-18T10:32:00.313+05:30")
},
{
"amount" : 10,
"date" : ISODate("2019-02-18T10:32:00.313+05:30")
}
]
},
/* 6 createdAt:23/05/2019, 14:42:54*/
{
"_id" : ObjectId("5ce66416ff87476e5eef0398"),
"tot_plafond" : 20,
"debtor_plafonds" : ObjectId("5cad84b93d124a151f633af0"),
"debtor_taks" : ObjectId("5c91c0f121a78f19d9eb3532"),
"transfers" : [
{
"amount" : 10,
"date" : ISODate("2019-02-18T10:32:00.313+05:30")
},
{
"amount" : 10,
"date" : ISODate("2019-02-18T10:32:00.313+05:30")
}
]
}
Дайте мне знать, что вы хотите получить результат, и я изменю запрос для этого.