我有集合score和scoreDetail,我想通过teacherId,scoreTypeId,studentId和subjectId对每个我尝试使用聚合的多个数组进行分组,但它仍然是错误的,我不知道。我已经设法对数组进行分组,但我不知道如何对数据进行分组,所以请帮助我
分数详情
/* 1 */
{
"_id" : "667d6f13c0e5693864390ee0",
"prefixId" : "667a7c3e04a229a7be807ae6",
"tranDate" : ISODate("2024-07-02T20:54:27.971+07:00"),
"sectionId" : "667d68ab313014638d27a01e",
"examName" : "Attendance 2024",
"scoreTypeId" : "667a7bc304a229a7be807acc",
"teacherId" : "667632f516284123d59de97b",
"branchId" : "ZgqaTMBFkEZ7PkRi5",
"comId" : "p6nB3E8gFCn98n6ZQ",
},
/* 2 */
{
"_id" : "667d6efcc0e569386439063c",
"prefixId" : "667a7c3e04a229a7be807ae6",
"tranDate" : ISODate("2024-07-02T20:54:04.011+07:00"),
"sectionId" : "667d68ab313014638d27a01e",
"examName" : "Attendance 2024",
"scoreTypeId" : "667a7bc304a229a7be807acc",
"teacherId" : "66276f6d07b416e047311ca1",
"branchId" : "ZgqaTMBFkEZ7PkRi5",
"comId" : "p6nB3E8gFCn98n6ZQ",
},
/* 3 */
{
"_id" : "667d6e83c0e569386438e8c6",
"prefixId" : "667a7c3e04a229a7be807ae6",
"tranDate" : ISODate("2024-07-01T20:52:03.693+07:00"),
"sectionId" : "667d68ab313014638d27a01e",
"examName" : "Attendance 2024",
"scoreTypeId" : "667a7bc304a229a7be807acc",
"teacherId" : "667632f516284123d59de97b",
"branchId" : "ZgqaTMBFkEZ7PkRi5",
"comId" : "p6nB3E8gFCn98n6ZQ",
},
/* 4 */
{
"_id" : "667d6e55c0e569386438d967",
"prefixId" : "667a7c3e04a229a7be807ae6",
"tranDate" : ISODate("2024-07-01T20:52:34.460+07:00"),
"sectionId" : "667d68ab313014638d27a01e",
"examName" : "Attendance 2024",
"scoreTypeId" : "667a7bc304a229a7be807acc",
"teacherId" : "66276f6d07b416e047311ca1",
"branchId" : "ZgqaTMBFkEZ7PkRi5",
"comId" : "p6nB3E8gFCn98n6ZQ",
}
分数详情
/* 1 */
{
"_id" : "667d6f14c0e5693864390ee3",
"tranDate" : ISODate("2024-07-02T20:54:27.971+07:00"),
"scoreId" : "667d6f13c0e5693864390ee0",
"studentId" : "666fe672d755bdf45ac5a4d4",
"scores" : [
{
"subjectId" : "66276f6d07b416e047311d73",
"value" : 0,
}
],
"branchId" : "ZgqaTMBFkEZ7PkRi5",
"comId" : "p6nB3E8gFCn98n6ZQ",
},
/* 2 */
{
"_id" : "667d6f14c0e5693864390ee2",
"tranDate" : ISODate("2024-07-02T20:54:27.971+07:00"),
"scoreId" : "667d6f13c0e5693864390ee0",
"studentId" : "66616dff7ec3fe1d9097c0b6",
"scores" : [
{
"subjectId" : "66276f6d07b416e047311d73",
"value" : 1,
}
],
"branchId" : "ZgqaTMBFkEZ7PkRi5",
"comId" : "p6nB3E8gFCn98n6ZQ",
},
/* 3 */
{
"_id" : "667d6efcc0e5693864390641",
"tranDate" : ISODate("2024-07-02T20:54:04.011+07:00"),
"scoreId" : "667d6efcc0e569386439063c",
"studentId" : "666fe672d755bdf45ac5a4d4",
"scores" : [
{
"subjectId" : "66276f6d07b416e047311d73",
"value" : 0,
}
],
"branchId" : "ZgqaTMBFkEZ7PkRi5",
"comId" : "p6nB3E8gFCn98n6ZQ",
},
/* 4 */
{
"_id" : "667d6efcc0e569386439063f",
"tranDate" : ISODate("2024-07-02T20:54:04.011+07:00"),
"scoreId" : "667d6efcc0e569386439063c",
"studentId" : "66616dff7ec3fe1d9097c0b6",
"scores" : [
{
"subjectId" : "66276f6d07b416e047311d73",
"value" : 2,
}
],
"branchId" : "ZgqaTMBFkEZ7PkRi5",
"comId" : "p6nB3E8gFCn98n6ZQ",
},
/* 5 */
{
"_id" : "667d6ea2c0e569386438f302",
"tranDate" : ISODate("2024-07-01T20:52:34.460+07:00"),
"scoreId" : "667d6e55c0e569386438d967",
"studentId" : "666fe672d755bdf45ac5a4d4",
"scores" : [
{
"subjectId" : "66276f6d07b416e047311d73",
"value" : 0,
}
],
"branchId" : "ZgqaTMBFkEZ7PkRi5",
"comId" : "p6nB3E8gFCn98n6ZQ",
},
/* 6 */
{
"_id" : "667d6ea2c0e569386438f300",
"tranDate" : ISODate("2024-07-01T20:52:34.460+07:00"),
"scoreId" : "667d6e55c0e569386438d967",
"studentId" : "66616dff7ec3fe1d9097c0b6",
"scores" : [
{
"subjectId" : "66276f6d07b416e047311d73",
"value" : 0,
}
],
"branchId" : "ZgqaTMBFkEZ7PkRi5",
"comId" : "p6nB3E8gFCn98n6ZQ",
},
/* 7 */
{
"_id" : "667d6e83c0e569386438e8cb",
"tranDate" : ISODate("2024-07-01T20:52:03.693+07:00"),
"scoreId" : "667d6e83c0e569386438e8c6",
"studentId" : "666fe672d755bdf45ac5a4d4",
"scores" : [
{
"subjectId" : "66276f6d07b416e047311d73",
"value" : 2,
}
],
"branchId" : "ZgqaTMBFkEZ7PkRi5",
"comId" : "p6nB3E8gFCn98n6ZQ",
},
/* 8 */
{
"_id" : "667d6e83c0e569386438e8c9",
"tranDate" : ISODate("2024-07-01T20:52:03.693+07:00"),
"scoreId" : "667d6e83c0e569386438e8c6",
"studentId" : "66616dff7ec3fe1d9097c0b6",
"scores" : [
{
"subjectId" : "66276f6d07b416e047311d73",
"value" : 0,
}
],
"branchId" : "ZgqaTMBFkEZ7PkRi5",
"comId" : "p6nB3E8gFCn98n6ZQ",
}
我的期待是这样的
[
{
"_id": "667d6ea2c0e569386438f302",
"teacherId": "66276f6d07b416e047311ca1",
"tranDate": ISODate("2024-07-01T20:52:34.460+07:00"),
"scoreId": "667d6e55c0e569386438d967",
"studentId": "666fe672d755bdf45ac5a4d4",
"scores": [
{
"tranDate": ISODate("2024-07-01T20:52:34.460+07:00"),
"subjectId": "66276f6d07b416e047311d73",
"value": 0,
},
{
"tranDate": ISODate("2024-07-02T20:54:04.011+07:00"),
"subjectId": "66276f6d07b416e047311d73",
"value": 0,
}
],
"branchId": "ZgqaTMBFkEZ7PkRi5",
"comId": "p6nB3E8gFCn98n6ZQ",
},
{
"_id": "667d6e83c0e569386438e8cb",
"teacherId": "667632f516284123d59de97b",
"tranDate": ISODate("2024-07-01T20:52:03.693+07:00"),
"scoreId": "667d6e83c0e569386438e8c6",
"studentId": "666fe672d755bdf45ac5a4d4",
"scores": [
{
"tranDate": ISODate("2024-07-01T20:52:03.693+07:00"),
"subjectId": "66276f6d07b416e047311d73",
"value": 2,
},
{
"tranDate": ISODate("2024-07-02T20:54:27.971+07:00"),
"subjectId": "66276f6d07b416e047311d73",
"value": 0,
}
],
"branchId": "ZgqaTMBFkEZ7PkRi5",
"comId": "p6nB3E8gFCn98n6ZQ",
},
/* 2 */
{
"_id": "667d6ea2c0e569386438f300",
"teacherId": "66276f6d07b416e047311ca1",
"tranDate": ISODate("2024-07-01T20:52:34.460+07:00"),
"scoreId": "667d6e55c0e569386438d967",
"studentId": "66616dff7ec3fe1d9097c0b6",
"scores": [
{
"tranDate": ISODate("2024-07-01T20:52:34.460+07:00"),
"subjectId": "66276f6d07b416e047311d73",
"value": 0,
},
{
"tranDate": ISODate("2024-07-02T20:54:04.011+07:00"),
"subjectId": "66276f6d07b416e047311d73",
"value": 2,
}
],
"branchId": "ZgqaTMBFkEZ7PkRi5",
"comId": "p6nB3E8gFCn98n6ZQ",
},
{
"_id": "667d6e83c0e569386438e8c9",
"teacherId": "667632f516284123d59de97b",
"tranDate": ISODate("2024-07-01T20:52:03.693+07:00"),
"scoreId": "667d6e83c0e569386438e8c6",
"studentId": "66616dff7ec3fe1d9097c0b6",
"scores": [
{
"tranDate": ISODate("2024-07-01T20:52:03.693+07:00"),
"subjectId": "66276f6d07b416e047311d73",
"value": 0,
},
{
"tranDate": ISODate("2024-07-02T20:54:27.971+07:00"),
"subjectId": "66276f6d07b416e047311d73",
"value": 1,
}
],
"branchId": "ZgqaTMBFkEZ7PkRi5",
"comId": "p6nB3E8gFCn98n6ZQ",
},
]
$lookup
- 连接 score 和 scoreDetail 集合并将结果作为 scores
数组返回。
1.1. $match
- 按 scoreId
过滤文档。
1.2. $unwind
- 将 scores
数组解构为多个文档。
1.3. $project
- 装饰输出文档。
$project
- 装饰输出文档。
db.score.aggregate([
{
"$lookup": {
"from": "scoreDetail",
"let": {
scoreId: "$_id"
},
"pipeline": [
{
$match: {
$expr: {
$eq: [
"$scoreId",
"$$scoreId"
]
}
}
},
{
$unwind: "$scores"
},
{
$project: {
_id: 0,
tranDate: "$tranDate",
subjectId: "$scores.subjectId",
value: "$scores.value"
}
}
],
"as": "scores"
}
},
{
$project: {
"_id": 1,
"teacherId": 1,
"tranDate": 1,
"scoreId": 1,
"studentId": 1,
"scores": 1,
"branchId": 1,
"comId": 1
}
}
])