如何将聚合用于模型?
How can I use Aggregate for models?
我有一个名为 Course 的模型,如下所示,其中有一个 section 模型(嵌入):
{
"_id": "624e8e6a870036ee6376d675",
"standardCode": "21321 JH 123",
"name": "JS programming bootcamp",
"description": "",
"start_date": "4/9/2022",
"picture": "course-135526.jpg",
"sections": [
"624e8eb9870036ee6376d690",
"624eda279290d7d518de58a9",
"624ee9a51fbdb9c1986134dd"
]
"createAt": "Thu - 4/8/2022"
},
{
"_id": "624e8e6a870036fd3276d213",
"standardCode": "232 ab 333",
"name": "React programming bootcamp",
"description": "",
"start_date": "4/9/2022",
"picture": "course-135526.jpg",
"sections": []
"createAt": "Thu - 4/8/2022"
}
部分模型如下:
{
"_id": "624e8eb9870036ee6376d690",
"name": "intro",
"description": "",
"price": 0,
"type": "classroom",
},
{
"_id": "624eda279290d7d518de58a9",
"name": "about js",
"description": "",
"price": 25000,
"type": "classroom",
},
{
"_id": "624ee9a51fbdb9c1986134dd",
"name": "define variable",
"description": "",
"price": 30000,
"type": "classroom",
}
这就是我的两个模型,我在这里写的课程和部分非常大,但这个信息对他们所有人来说都是好的。
所以,我想从输出中得到这个:
{
"_id": "624e8e6a870036fd3276d213",
"sumPrice": 0,
"numSection": 0,
"standardCode": "232 ab 333",
"name": "React programming bootcamp",
"description": "",
"start_date": "4/9/2022",
"picture": "course-135526.jpg",
"createAt": "Thu - 4/8/2022"
},
{
"_id": "624e8e6a870036ee6376d675",
"sumPrice": 55000,
"numSection": 3,
"standardCode": "21321 JH 123",
"name": "JS programming bootcamp",
"description": "",
"start_date": "4/9/2022",
"picture": "course-135526.jpg",
"createAt": "Thu - 4/8/2022"
}
其中 SumPrice 是部分价格总和,numSection 是部分数量总和。
db.Course.aggregate([
{
$lookup: {
from: "Section",
localField: "sections",
foreignField: "_id",
as: "sections"
}
},
{
$set: {
sumPrice: {
$sum: "$sections.price"
},
numSection: {
$size: "$sections"
}
}
},
{
$unset: "sections"
}
])
我有一个名为 Course 的模型,如下所示,其中有一个 section 模型(嵌入):
{
"_id": "624e8e6a870036ee6376d675",
"standardCode": "21321 JH 123",
"name": "JS programming bootcamp",
"description": "",
"start_date": "4/9/2022",
"picture": "course-135526.jpg",
"sections": [
"624e8eb9870036ee6376d690",
"624eda279290d7d518de58a9",
"624ee9a51fbdb9c1986134dd"
]
"createAt": "Thu - 4/8/2022"
},
{
"_id": "624e8e6a870036fd3276d213",
"standardCode": "232 ab 333",
"name": "React programming bootcamp",
"description": "",
"start_date": "4/9/2022",
"picture": "course-135526.jpg",
"sections": []
"createAt": "Thu - 4/8/2022"
}
部分模型如下:
{
"_id": "624e8eb9870036ee6376d690",
"name": "intro",
"description": "",
"price": 0,
"type": "classroom",
},
{
"_id": "624eda279290d7d518de58a9",
"name": "about js",
"description": "",
"price": 25000,
"type": "classroom",
},
{
"_id": "624ee9a51fbdb9c1986134dd",
"name": "define variable",
"description": "",
"price": 30000,
"type": "classroom",
}
这就是我的两个模型,我在这里写的课程和部分非常大,但这个信息对他们所有人来说都是好的。 所以,我想从输出中得到这个:
{
"_id": "624e8e6a870036fd3276d213",
"sumPrice": 0,
"numSection": 0,
"standardCode": "232 ab 333",
"name": "React programming bootcamp",
"description": "",
"start_date": "4/9/2022",
"picture": "course-135526.jpg",
"createAt": "Thu - 4/8/2022"
},
{
"_id": "624e8e6a870036ee6376d675",
"sumPrice": 55000,
"numSection": 3,
"standardCode": "21321 JH 123",
"name": "JS programming bootcamp",
"description": "",
"start_date": "4/9/2022",
"picture": "course-135526.jpg",
"createAt": "Thu - 4/8/2022"
}
其中 SumPrice 是部分价格总和,numSection 是部分数量总和。
db.Course.aggregate([
{
$lookup: {
from: "Section",
localField: "sections",
foreignField: "_id",
as: "sections"
}
},
{
$set: {
sumPrice: {
$sum: "$sections.price"
},
numSection: {
$size: "$sections"
}
}
},
{
$unset: "sections"
}
])