在$ skip,$ limit,$ project和$ lookup上使用聚集管道进行MongoDB查询

时间:2018-10-23 13:37:03

标签: php mongodb nosql aggregate

我想通过PHP在单个聚合上一起实现$skip, $limit, $project & $lookup

我的以下原始MongoDB查询在从终端命中时工作正常,我想用PHP来实现

db.getCollection("tbl_players").aggregate([

    {$skip: 10},
    {$limit: 10},
    {$project: {
        "fullname":1,
        "fullname_ar":1,
        "country.name":1,
        "country.name_ar":1,
        "country.ios2":1,
        "_id":1,
    }},
    {$lookup: {
        from: "tbl_countries",
        localField: "country_id",
        foreignField: "_id",
        as: "country"
    }}

])

我已经尝试了多种方法来使它起作用,这是其中的两个

这是我要以$pipeline传递给MongoDB\Collection::aggregate()的PHP数组上的输出

array (size=4)
  '$skip' => int 10
  '$limit' => int 10
  '$project' => 
    array (size=6)
      '_id' => int 0
      'fullname' => int 1
      'fullname_ar' => int 1
      'country.name' => int 1
      'country.name_ar' => int 1
      'country.ios2' => int 1
  '$lookup' => 
    array (size=4)
      'from' => string 'tbl_countries' (length=13)
      'localField' => string 'country_id' (length=10)
      'foreignField' => string '_id' (length=3)
      'as' => string 'country' (length=7)

在上面的数组中,出现以下错误

  

管道阶段规范对象必须只包含一个字段。

关注数组输出

array (size=4)
  0 => 
    array (size=1)
      '$skip' => int 10
  1 => 
    array (size=1)
      '$limit' => int 10
  2 => 
    array (size=1)
      '$project' => 
        array (size=6)
          '_id' => int 0
          'fullname' => int 1
          'fullname_ar' => int 1
          'country.name' => int 1
          'country.name_ar' => int 1
          'country.ios2' => int 1
  3 => 
    array (size=1)
      '$lookup' => 
        array (size=4)
          'from' => string 'tbl_countries' (length=13)
          'localField' => string 'country_id' (length=10)
          'foreignField' => string '_id' (length=3)
          'as' => string 'country' (length=7)

在上面的数组中,出现以下错误

  

“管道”数组的每个元素必须是一个对象

PHP代码重现错误

$more_where[]['$skip'] = 10;
$more_where[]['$limit'] = 10;
$more_where[]['$project'] = array(
                  '_id' => 0,
                  'fullname' => 1,
                  'fullname_ar' => 1,
                  'country.name' => 1,
                  'country.name_ar' => 1,
                  'country.ios2' => 1,
                );
$where['$lookup'] = array(
                  'from' => 'tbl_countries',
                  'localField' => 'country_id',
                  'foreignField' => '_id',
                  'as' => 'country',
                );


$collection->aggregate( array($where, $more_where) );

这是我的PHP,MongoDB扩展和MongoDB的版本

PHP版本:7.0.22

驱动程序版本:1.5.1

MongoDB版本:3.4.7

1 个答案:

答案 0 :(得分:0)

应该是:

$more_where = [];
$more_where[]['$skip'] = 10;
$more_where[]['$limit'] = 10;
$more_where[]['$project'] = array(
                  '_id' => 0,
                  'fullname' => 1,
                  'fullname_ar' => 1,
                  'country.name' => 1,
                  'country.name_ar' => 1,
                  'country.ios2' => 1,
                );
$more_where[]['$lookup'] = array(
                  'from' => 'tbl_countries',
                  'localField' => 'country_id',
                  'foreignField' => '_id',
                  'as' => 'country',
                );


$collection->aggregate( $more_where );