根据 mongo db php 中嵌入文档的最后一个元素的字段查找

Find on the basis of field of last element of embedded document in mongo db php

我有一个集合 studentTbl,其中包含像

这样的记录

记录 1

 "_id": ObjectId("5b45d89bbbc51e2c2c006973")
 "first_name": "Pooja",
 ...
 contact_details[
    {
     ..
    },
    {
     ..
    }
 ]
  transport_details[
    {
      allotment_id:"68998546878",
      ..
      status:"Inactive"
    },
    {
      allotment_id:"25799856890",
      ..
      status:"Active"
    }
 ]
}

记录 2

  "_id": ObjectId("5b45d89bbbc51e2c2533")
 "first_name": "Poornima",
 ...
 contact_details[
    {
     ..
    },
    {
     ..
    }
 ]
  transport_details[
    {
      allotment_id:"68998546878",
      ..
      status:"Inactive"
    }
 ]
}

记录 3

 "_id": ObjectId("5b45d89bbbc51e2c2c00646")
 "first_name": "Poonam",
 ...
 contact_details[
    {
     ..
    },
    {
     ..
    }
 ]
  transport_details[
    {
      allotment_id:"68998546878",
      ..
      status:"Inactive"
    },
    {
      allotment_id:"25799856890",
      ..
      status:"Active"
    }
  ]
}

我正在尝试调整以下代码行,以获取那些 first_name 或 middle_name 或 last_name 包含 "poo" 且在最后一个元素内的学生嵌入文档 transport_details,状态应为 "Active"。如何编写这样一个查询,它会根据姓名查找学生并遍历嵌入文档的最后一个元素 transport_details 并检查状态是否为活动?例如在上面的集合中,应该返回 pooja 和 poonam。

代码是这样的

      // default if nothing is filled
      $query = array("schoolId"=>  new MongoDB\BSON\ObjectID($this->schoolId));

     // if name = filled, class = null, year = null 
     if(!empty($this->name) && empty($this->academicYearName) && empty($this->programId))
     {

           $param = preg_replace('!\s+!', ' ', $this->name);
           $arg = trim($param);

           $query =  array(
                   '$or' => array(
                                    array("first_name" => new MongoDB\BSON\Regex($arg, 'i')),
                                    array("middle_name" => new MongoDB\BSON\Regex($arg, 'i')),
                                    array("last_name" => new MongoDB\BSON\Regex($arg, 'i')),
                                    array("registration_temp_perm_no" => $arg)
                                  ),

                        "schoolId"=>  new MongoDB\BSON\ObjectID($this->schoolId)
                    );

     }
    ...
    ...
    ...
     $pipeline = array(
        array(
            '$match' => $query
        ),
        array(
            '$lookup' => array(
                'from' => 'programTbl',
                'localField' => 'classId',
                'foreignField' => '_id',
                'as' => 'ClassDetails'
            )
        ),
     );

    try
    {
        $cursor = $this->collection->aggregate($pipeline);
    } 
    catch (Exception $e) {

    }

    return $cursor->toArray();

请注意,实际代码包含更多条件 $query 变量...

您可以在管道中添加以下阶段

array(
  "$match" => array(
    "$expr" => array(
      "$and" => [
        array(
          "$or" => [
            array( "$eq" => [ array( "$strcasecmp" => [ "$first_name", "Poo" ] ), 1 ] ),
            array( "$eq" => [ array( "$strcasecmp" => [ "$last_name", "Poo" ] ), 1 ] ),
            array( "$eq" => [ array( "$strcasecmp" => [ "$middle_name", "Poo" ] ), 1 ] ),
          ],
        ),
        array(
          "$eq" => [
            array( "$arrayElemAt" => [ array( "$slice"=> [ "$transport_details.status", -1 ] ), 0 ] ),
            "Active"
          ]
        )
      ]
    )
  )
)

您可以在 3.6 中使用以下查询。

array(
    '$or' => array(
        array("first_name" => new MongoDB\BSON\Regex("/poo/i")),
        array("middle_name" => new MongoDB\BSON\Regex("/poo/i")),
        array("last_name" => new MongoDB\BSON\Regex("/poo/i"))
    ),
    "$expr" => array(
        "$eq" => array(
            array("$arrayElemAt" => array("$transport_details.status", -1)),
            "Active"
        )
    )
);