0

我在 mongo db 中有一个集合,例如

        "_id": ObjectId("5aa662b0d2ccda095400022f"),
        "EmployeeNumber": "12345",
        "JobTitle": ObjectId("5a7c2008d2ccda04d000606f"),
        "Instructor": NumberInt(1),
        "Department": ObjectId("5a8173e6d2ccda13240015a4"),
        "FirstName": "Aasiya",
        "MiddleName": "Rashid",
        "LastName": "Khan"

我正在尝试使用以下代码行搜索一个参数

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

   var_dump($arg);

    $cursor= $this->collection->aggregate(
        array(
            array(
                      '$project' => array(
                      'FullName' => array('$concat' => array('$FirstName',  ' ',  '$MiddleName', ' ', '$LastName')),
                      'FirstMiddle' => array('$concat' => array('$FirstName',  ' ',  '$MiddleName')),
                      'FirstLast' => array('$concat' => array('$FirstName',  ' ',  '$LastName')),
                      'Employee' => '$$ROOT'
                  )
                ),
             array(
               '$match' =>
                     array('$or' =>
                        array(
                        array("Employee.FullName" => new MongoRegex("/$arg/i")),
                        array("Employee.FirstLast" => new MongoRegex("/$arg/i")),
                        array("Employee.FirstMiddle" => new MongoRegex("/$arg/i")),
                        array("Employee.EmployeeNumber" => new MongoRegex("/^$arg/i"))
                          )
                        )
                      ),
                    )
                 );

     return $cursor->toArray();

它返回空数组,如“array(0) { }”。我在早期版本的 mongo db 中尝试过类似的方法,上面的查询工作正常。现在我是 mongo db 3.6,它在这个版本中不起作用。如果我在参数 searchCriteria 中给出“aa”,它就不起作用。

请帮忙!!!

4

1 回答 1

1

最后阶段应该是

         array(
           '$match' =>
                 array('$or' =>
                    array(
                    array("FullName" => new MongoDB\BSON\Regex($arg, 'i')),
                    array("FirstLast" => new MongoDB\BSON\Regex($arg, 'i')),
                    array("FirstMiddle" => new MongoDB\BSON\Regex($arg, 'i')),
                    array("Employee.EmployeeNumber" => new MongoDB\BSON\Regex($arg, 'i'))
                      )
                    )
                  ),
于 2018-04-06T11:10:05.957 回答