在第二个(学生)表中查找匹配所有传入输出记录mongodb聚合时遇到问题
第二个(学生)表中的查找与第一个(测试)表的所有传入输出记录相匹配,我面临一个问题。我有两个集合“测试”和“学生”。“测试”集合包含所有学校测试,“学生”表包含学生参加的测试。学生表包含“pastTest”(过去参加的测试状态为“通过”或“失败”)数组。我想检索通过所有传入测试的学生(我们从测试表中检索) 测试表:_id(主ket) student.passtests.testId(需要与test.\u id匹配) 测试文档:在第二个(学生)表中查找匹配所有传入输出记录mongodb聚合时遇到问题,mongodb,aggregation-framework,Mongodb,Aggregation Framework,第二个(学生)表中的查找与第一个(测试)表的所有传入输出记录相匹配,我面临一个问题。我有两个集合“测试”和“学生”。“测试”集合包含所有学校测试,“学生”表包含学生参加的测试。学生表包含“pastTest”(过去参加的测试状态为“通过”或“失败”)数组。我想检索通过所有传入测试的学生(我们从测试表中检索) 测试表:_id(主ket) student.passtests.testId(需要与test.\u id匹配) 测试文档: { "_id" : ObjectId(&
{
"_id" : ObjectId("5c9b5c1005729b2bf23f3290"),
"testDate" : {
"term" : 1,
"week" : 7
},
"retestDate" : {
"term" : 1,
"week" : 10
},
"testOrder" : "1.1",
"testDateScheduled" : true,
"retestDateScheduled" : true
}
{
"_id" : ObjectId("5c92dd994e8e6b2c1647d0d0"),
"completedYears" : [],
"firstName" : "Andrew",
"lastName" : "Jonhson",
"teacherId" : ObjectId("5bf36b1076696374e65feb4f"),
"yearGroup" : "0",
"schoolId" : 40001,
"currentTest" : ObjectId("5c9b5c1005729b2bf23f3290"),
"pastTests" : [
{
"_id" : ObjectId("5d3570645045863d373f6db1"),
"testId" : ObjectId("5c9b5c1005729b2bf23f3290"),
"status" : "pass"
},
{
"_id" : ObjectId("5d425af07708f5636c3bec1c"),
"testId" : ObjectId("5c9b5fc460e39c2c58e44109"),
"status" : "pass"
},
{
"_id" : ObjectId("5d5e54a875fab079f4d03570"),
"testId" : ObjectId("5c9b6492bb581c2ceb553fef"),
"status" : "fail"
},
],
"createdAt" : ISODate("2019-03-21T00:40:57.401Z"),
"updatedAt" : ISODate("2020-09-24T19:55:38.291Z"),
"__v" : 0,
"holdTests" : [],
"completedTests" : [],
"className" : "dd",
}
db.getCollection('tests').aggregate([
{
$match: {
yearGroup: '-1',
$or : [
{
$and: [
{'retestDateScheduled': true},
{ 'retestDate.term': { $lt: 4 } },
]
},
{
$and: [
{'testDateScheduled': true},
{ 'testDate.term': { $lt: 4 } },
]
}
]
}
},
{
$lookup: {
from: 'students',
let: {testId: '$_id', schoolId: 49014, yearGroup: '-1'},
pipeline: [
]
}
}
])
学生文档:
{
"_id" : ObjectId("5c9b5c1005729b2bf23f3290"),
"testDate" : {
"term" : 1,
"week" : 7
},
"retestDate" : {
"term" : 1,
"week" : 10
},
"testOrder" : "1.1",
"testDateScheduled" : true,
"retestDateScheduled" : true
}
{
"_id" : ObjectId("5c92dd994e8e6b2c1647d0d0"),
"completedYears" : [],
"firstName" : "Andrew",
"lastName" : "Jonhson",
"teacherId" : ObjectId("5bf36b1076696374e65feb4f"),
"yearGroup" : "0",
"schoolId" : 40001,
"currentTest" : ObjectId("5c9b5c1005729b2bf23f3290"),
"pastTests" : [
{
"_id" : ObjectId("5d3570645045863d373f6db1"),
"testId" : ObjectId("5c9b5c1005729b2bf23f3290"),
"status" : "pass"
},
{
"_id" : ObjectId("5d425af07708f5636c3bec1c"),
"testId" : ObjectId("5c9b5fc460e39c2c58e44109"),
"status" : "pass"
},
{
"_id" : ObjectId("5d5e54a875fab079f4d03570"),
"testId" : ObjectId("5c9b6492bb581c2ceb553fef"),
"status" : "fail"
},
],
"createdAt" : ISODate("2019-03-21T00:40:57.401Z"),
"updatedAt" : ISODate("2020-09-24T19:55:38.291Z"),
"__v" : 0,
"holdTests" : [],
"completedTests" : [],
"className" : "dd",
}
db.getCollection('tests').aggregate([
{
$match: {
yearGroup: '-1',
$or : [
{
$and: [
{'retestDateScheduled': true},
{ 'retestDate.term': { $lt: 4 } },
]
},
{
$and: [
{'testDateScheduled': true},
{ 'testDate.term': { $lt: 4 } },
]
}
]
}
},
{
$lookup: {
from: 'students',
let: {testId: '$_id', schoolId: 49014, yearGroup: '-1'},
pipeline: [
]
}
}
])
查询:
{
"_id" : ObjectId("5c9b5c1005729b2bf23f3290"),
"testDate" : {
"term" : 1,
"week" : 7
},
"retestDate" : {
"term" : 1,
"week" : 10
},
"testOrder" : "1.1",
"testDateScheduled" : true,
"retestDateScheduled" : true
}
{
"_id" : ObjectId("5c92dd994e8e6b2c1647d0d0"),
"completedYears" : [],
"firstName" : "Andrew",
"lastName" : "Jonhson",
"teacherId" : ObjectId("5bf36b1076696374e65feb4f"),
"yearGroup" : "0",
"schoolId" : 40001,
"currentTest" : ObjectId("5c9b5c1005729b2bf23f3290"),
"pastTests" : [
{
"_id" : ObjectId("5d3570645045863d373f6db1"),
"testId" : ObjectId("5c9b5c1005729b2bf23f3290"),
"status" : "pass"
},
{
"_id" : ObjectId("5d425af07708f5636c3bec1c"),
"testId" : ObjectId("5c9b5fc460e39c2c58e44109"),
"status" : "pass"
},
{
"_id" : ObjectId("5d5e54a875fab079f4d03570"),
"testId" : ObjectId("5c9b6492bb581c2ceb553fef"),
"status" : "fail"
},
],
"createdAt" : ISODate("2019-03-21T00:40:57.401Z"),
"updatedAt" : ISODate("2020-09-24T19:55:38.291Z"),
"__v" : 0,
"holdTests" : [],
"completedTests" : [],
"className" : "dd",
}
db.getCollection('tests').aggregate([
{
$match: {
yearGroup: '-1',
$or : [
{
$and: [
{'retestDateScheduled': true},
{ 'retestDate.term': { $lt: 4 } },
]
},
{
$and: [
{'testDateScheduled': true},
{ 'testDate.term': { $lt: 4 } },
]
}
]
}
},
{
$lookup: {
from: 'students',
let: {testId: '$_id', schoolId: 49014, yearGroup: '-1'},
pipeline: [
]
}
}
])
注意:初始匹配查询返回第一学期的所有测试,现在我必须检索通过第一学期所有测试的学生
查找阶段处于挂起状态-第二个(学生)表中的查找遇到问题,该表匹配第一个(测试)集合的所有传入输出记录
提前谢谢 试试这个:
db.tests.aggregate([
{
$match:{
//你的比赛条件
}
},
{
$group:{
_id:null,
术语_1_testid:{$push:$\u id},
测试计数:{$sum:1}
}
},
{
$lookup:{
来自:“学生”,
let:{term_1_testIds:'$term_1_testIds',学号:40001,totalTestCount:“$test_count”},
管道:[
{
$match:{
$expr:{$eq:[“$schoolId”,“$$schoolId”]}
}
},
{$unwind:“$pastTests”},
{
$match:{
“pastTests.status”:“通过”,
$expr:{$in:[“$pastTests.testId”,“$$term_1_testId”]}
}
},
{
$group:{
_id:“$\u id”,
firstName:{$first:$firstName},
yearGroup:{$first:$yearGroup},
schoolId:{$first:$schoolId},
currentTest:{$first:$currentTest},
passedTestCount:{$sum:1},
pastTests:{$push:$pastTests}
}
},
{
$match:{
$expr:{$eq:[“$passedTestCount”,“$$totalTestCount”]}
}
}
],
as:“学生”
}
}
]);
输出:
{
“_id”:空,
“术语1测试”:[
目标(“5c9b5c1005729b2bf23f3290”),
ObjectId(“5c9b5fc460e39c2c58e44109”),
目标(“5c9b6492bb581c2ceb553fef”)
],
“测试计数”:3,
“学生”:[
{
“_id”:ObjectId(“5c92dd994e8e6b2c1647d0d1”),
“firstName”:“Dheemanth”,
“yearGroup”:“0”,
“学校ID”:40001,
“当前测试”:ObjectId(“5c9b5c1005729b2bf23f3290”),
“通过测试计数”:3,
“过去的测试”:[
{
“_id”:ObjectId(“5d3570645045863d373f6db1”),
“testId”:ObjectId(“5c9b5c1005729b2bf23f3290”),
“状态”:“通过”
},
{
“_id”:ObjectId(“5d425af07708f5636c3bec1c”),
“testId”:ObjectId(“5c9b5fc460e39c2c58e44109”),
“状态”:“通过”
},
{
“_id”:ObjectId(“5d5e54a875fab079f4d03570”),
“testId”:ObjectId(“5c9b6492bb581c2ceb553fef”),
“状态”:“通过”
}
]
}
]
}
这就是我的测试集合的样子
/*1创建日期:2019年3月27日下午5:24:58*/
{
“_id”:ObjectId(“5C9B6492BB581CEB553FEF”),
“名称”:“测试3”
},
/*创建日期:2019年3月27日下午5:04:28*/
{
“_id”:ObjectId(“5c9b5fc460e39c2c58e44109”),
“名称”:“测试2”
},
/*创建日期:2019年3月27日下午4:48:40*/
{
“_id”:ObjectId(“5c9b5c1005729b2bf23f3290”),
“名称”:“测试1”
}
这就是我的学生
收藏的样子:
/*1创建日期:2019年3月21日上午6:10:57*/
{
“_id”:ObjectId(“5c92dd994e8e6b2c1647d0d1”),
“firstName”:“Dheemanth”,
“yearGroup”:“0”,
“学校ID”:40001,
“当前测试”:ObjectId(“5c9b5c1005729b2bf23f3290”),
“过去的测试”:[
{
“_id”:ObjectId(“5d3570645045863d373f6db1”),
“testId”:ObjectId(“5c9b5c1005729b2bf23f3290”),
“状态”:“通过”
},
{
“_id”:ObjectId(“5d425af07708f5636c3bec1c”),
“testId”:ObjectId(“5c9b5fc460e39c2c58e44109”),
“状态”:“通过”
},
{
“_id”:ObjectId(“5d5e54a875fab079f4d03570”),
“testId”:ObjectId(“5c9b6492bb581c2ceb553fef”),
“状态”:“通过”
}
]
},
/*创建日期:2019年3月21日,上午6:10:57*/
{
“_id”:ObjectId(“5c92dd994e8e6b2c1647d0d0”),
“名字”:“安德鲁”,
“yearGroup”:“0”,
“学校ID”:40001,
“当前测试”:ObjectId(“5c9b5c1005729b2bf23f3290”),
“过去的测试”:[
{
“_id”:ObjectId(“5d3570645045863d373f6db1”),
“testId”:ObjectId(“5c9b5c1005729b2bf23f3290”),
“状态”:“通过”
},
{
“_id”:ObjectId(“5d425af07708f5636c3bec1c”),
“testId”:ObjectId(“5c9b5fc460e39c2c58e44109”),
“状态”:“通过”
},
{
“_id”:ObjectId(“5d5e54a875fab079f4d03570”),
“testId”:ObjectId(“5c9b6492bb581c2ceb553fef”),
“状态”:“失败”
}
]
}
A