我正在对带有活动表的学生表进行嵌套的查找和投影查询,该表提供除一个字段(即“ studentsForSmallShirt”)以外的所需字段。该字段为空。
代码是
$query = ["is_temp_deleted"=>0, "funRun" => "Yes"];
$pipeline = array(
array(
'$match' => $query
),
array(
'$lookup' => array(
....
),
['$lookup'=> [
....
]],
['$project' => [
'_id' => 1.0,
'activityName' => 1.0,
'studentsOfWeekgroup' => [
'$filter' => [
'input' => '$Students',
'as' => 'item',
'cond' => [
....
]
]
],
..
]],
[
'$lookup' => [
....
]
],
[
'$lookup' => [
....
]
],
[
'$lookup' => [
...
]
],
['$project' => [
'_id' => 1.0,
'activityName' => 1.0,
'studentsOfWeekgroup' => 1.0,
'studentsForSmallShirt' => [
'$filter' => [
'input' => '$studentsOfWeekgroup',
'as' => 'items',
'cond' => [
'$and' => [
['$eq' =>['$$items.activity_details.shirtSize','S']],
['$eq' =>['$$items.activity_details.activityId','$_id']],
['$eq' =>['$$items.activity_details.weekgroupId', new MongoDB\BSON\ObjectID($this->id)]],
]
]
]
],
...
]],
);
$cursor = $this->db->activitiesTbl->aggregate($pipeline)->toArray();
我已经在控制台中检查到“ studentsOfWeekgroup”中的数据显示为
0 =>
"_id": ObjectId("5bf518378d03ec0b2400536f"),
"ssn_Last_four": "0001",
"first_name": "Thomas",
"activity_details": [
{
"studentId": ObjectId("5bf518378d03ec0b2400536f"),
"funrun": "Yes",
"weekgroupId": ObjectId("5bf50bd48d03ec0b2400536d"),
"activityId": ObjectId("5c10a0e08d03ec1834001c7a"),
"attending": "Yes",
"shirtSize": "M",
"id": ObjectId("5c10e5ae3250041188005dde")
},
{
"studentId": ObjectId("5bf518378d03ec0b2400536f"),
"funrun": "Yes",
"weekgroupId": ObjectId("5bf50bd48d03ec0b2400536d"),
"activityId": ObjectId("5c10a0e08d03ec1834001c6d"),
"attending": "Yes",
"shirtSize": "S",
"id": ObjectId("5c13338032500409f4007cf3")
}
]
1=>
"_id": ObjectId("5bf518378d03ec0b2400536e"),
....
....
嵌入的文档“ activity_details”包含学生的所有活动。
现在在上一个投影中,我正在基于studentsOfWeekgroup筛选“ studentsForSmallShirt”,即那些选择了shirtSize:“ S”的学生,并且活动ID在当前投影中存在,并且weekgroupId在变量$ this-> id中传递。 “ studentsForSmallShirt”上的过滤器不起作用。它总是空白。请帮忙!!!
请注意,有些学生的“ activity_details”内的活动不包含“ shirtSize”字段,并且有些学生的Activity_details嵌入式文档未分配。