使用php mongodb进行$unwind和$lookup

使用php mongodb进行$unwind和$lookup,mongodb,mongodb-query,aggregation-framework,mongodb-php,php-mongodb,Mongodb,Mongodb Query,Aggregation Framework,Mongodb Php,Php Mongodb,我在mongodb有一张学生桌 { "_id": ObjectId("5baa85041d7859f40d000029"), "Name": "John Doe", "RollNo": 12, "Class": "Ist" .... } 我还有另一张桌子 { "_id": ObjectId("5baa818d1d78594010000029"), "Name": "New yor

我在mongodb有一张学生桌

       {
      "_id": ObjectId("5baa85041d7859f40d000029"),
       "Name": "John Doe",
       "RollNo": 12,
       "Class": "Ist"
        ....
       }
我还有另一张桌子

   {
   "_id": ObjectId("5baa818d1d78594010000029"),
   "Name": "New york City",
   "StopDetails": [
   {
    "StopId": "abc777",
    "Name": "Block no 3"
   },
   {
   "StopId": "abc888",
   "Name": "Block no 4"
   }
 ],
"NumberOfSeats": "10",
"StudentDetails": [
 {
   "StudentId": ObjectId("5baa85041d7859f40d000029"),
   "VehicleId": "7756"
  },
  {
   "StudentId": ObjectId("5baa85f61d7859401000002a"),
   "VehicleId": "7676"
 }
 ]
}
我正在使用mongodb 3.6平台。在获得在线帮助后,我正在使用以下代码行

      $query = ['_id' => new MongoDB\BSON\ObjectID($this->id)];
      $cursor = $this->db->TblRoute->aggregate([
      ['$match' => $query],
      ['$lookup' => [
         'from' => "TblStudent",
         'let' => ['studentid' => '$StudentDetails.StudentId'],
         'pipeline' => [
         ['$match' => ['$expr'=> ['$in' => ['$_id','$$studentid' ]]]],
         ['$project' => ['Name'=> 1,'RollNo' => 1, '_id'=> 1]]
         ],
         'as' => 'studentObjects',
       ]],
       ['$unwind'=> '$studentObjects' ],
        // Group back to arrays
       [ '$group'=> [
               'StudentDetails.StudentId'=> '$_id',
                'StudentDetails.StudentData'=> [ '$push'=> '$studentObjects' ]
       ]]

     ]);
它正在抛出错误消息Uncaught exception'MongoDB\Driver\exception\RuntimeException',消息'The field'StudentDetails.StudentId'必须是累加器对象'

基本上,我想使用lookup aggregation从另一个表中获取数据。因此,在联机帮助和进一步研究之后,我能够编写上述代码。这里的主要问题是$unwind语句之前的代码将输出生成为单独的数组。如果我们在$lookup中写入'as'=>'StudentDetails.studentObjects',则数据会被覆盖使用新数据进行写入,从而导致其他字段(如vehicleid等)的丢失 所以在研究之后,我尝试添加$group将其放回StudentDetails嵌入文档中

期望输出

        {
"_id": ObjectId("5baa818d1d78594010000029"),
"Name": "New york City",
"StopDetails": [
     .....
  ],
   "StudentDetails": [
   {
   "StudentId": ObjectId("5baa85041d7859f40d000029"),
   "VehicleId": "7756",
   "StudentData": [
      "Name": ..
      "RollNo":...
    ]
    },
    {
    "StudentId": ObjectId("5baa85f61d7859401000002a"),
    "VehicleId": "7676",
     "StudentData": [
      "Name": ..
      "RollNo":...
    ]
   }
   ]
  }

请帮我解决这个问题

你可以这样做

$cursor = $this->db->TblRoute->aggregate([
  [ "$match" => $query ],
  [ "$unwind": "$StudentDetails" ],
  [ "$lookup" => [
    "from" => "TblStudent",
    "let" => [ "studentid" => "$StudentDetails.StudentId" ],
    "pipeline" => [
      [ "$match" => [ "$expr"=> [ "$eq" => [ "$_id", "$$studentid" ]]]],
      [ "$project" => [ "Name"=> 1, "RollNo" => 1, "_id"=> 1 ]]
    ],
    "as" => "StudentDetails.StudentId"
  ]],
  [ "$unwind": "$StudentDetails.StudentId" ],
  [ "$group"=> [
    "_id"=> "$_id",
    "Name": [ "$first"=> "$Name" ],
    "StopDetails": [ "$first"=> "$StopDetails" ],
    "StudentDetails"=> [ "$push"=> "$StudentDetails" ]
  ]]
])

现在它抛出错误致命错误:未捕获异常“MongoDB\Driver\exception\RuntimeException”,消息“组规范必须包含一个_id”更新答案请再次检查是否正常,但现在它删除了其他字段“Name”:“New york City”,“StopDetails”:[…],我也想保留它们。请帮助更新答案。使用我上面使用的
$first
。$first用于放置必填字段