我有一个下面的猫鼬模型。我想订购一些子文档,并按日期进行过滤(例如,仅最后10个子文档)
TotalPlafond有一个数字(tot_plafond
),以及每个债务人的子Plafonds列表。 tot_plafond
已更新,计算出debtor_plafond
的列表,该列表是对所有传输进行求和而得出的。
当我询问TotalPlafond时,我只希望有最近的10次转账,所以我应该按日期订购转账,只取最后的10次。
我可能还需要在2个日期之间进行一些转移。
我怎么能用猫鼬做到这一点?
这显然行不通。
// this just orders resulting docs, not subdocs
TotalPlafond.findById(id)
.sort('debtor_plafonds.debtor_taks.transfers.date')
这是你的模特:
/* ==== TotalPlafond Mongoose Model ==== */
{
tot_plafond : {type : Number},
debtor_plafonds : [
{
debtor_id : {type: ObjectID},
debtor_plafond : {type : Number},
debtor_taks: [
{
task_id : {type : ObjectID},
transfers : [
{
amount : {type : Number},
date : {type : Date}
}
]
}
]
}
]
}
我想带一个TotalPlafond(例如TotalPlafond.findById(id)
),所有的债务人已清盘,但每个债务人的每个债务人任务的最后10次转移
答案 0 :(得分:0)
您可以在下面尝试:
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")
}
]
}
让我知道您想要的结果,我将为此修改查询。