【问题标题】:Use of pattern $match $in in aggregate function of Mongo PHP LibraryMongo PHP 库聚合函数中模式 $match $in 的使用
【发布时间】:2017-12-02 05:55:17
【问题描述】:

我在我的代码中使用 Mongo PHP 库:https://github.com/mongodb/mongo-php-library/

我正在尝试使用 Mongo PHP 库中的聚合函数向 MongoDB 发送以下查询:

块引用

db.customers.aggregate(
{$match: {Country: "India", Email: {$in: [/.*@gmail.com*/,/.*@hotmail.com*/] } } },
{$group : {_id : {Email: "Email"}, "count" : {$sum : 1} } },
{$sort : {"count" : -1} }
)

此查询在 MongoDB 服务器上运行良好。但在 PHP 代码中它不起作用。

块引用

$query = array(
['$match' => ['Country' => "India", 
              'Email' => ['$in' => ['/.*@gmail.com*/','/.*@hotmail.com*/']]
             ]
],
['$group' => ['_id' => ['Email' => '$Email']
              'count' => ['$sum' => 1]
             ]
],
['$sort' => ['count' => -1]]
);
$cursor = $this->customers->aggregate($query);

$in 数组不适用于上述通配符模式。它不显示任何错误或结果。但它适用于确切的电子邮件。如何在 $match 下的 $in 数组中提及模式

请建议我如何在 Mongo PHP 库中提交通配符模式。

【问题讨论】:

  • 编辑:['_id' => ['Email' => '$Email'],

标签: php mongodb


【解决方案1】:

试试这个

$query = array(
array('$match' => array('Country' => "India",'Email' => 
    array('$in' => array('/.*@gmail.com*/','/.*@hotmail.com*/'))
)),

array('$group' =>  array('_id' =>  array('Email' => '$Email')
              'count' =>  array('$sum' => 1)
             )
),
 array('$sort' =>  array('count' => -1))
);

【讨论】:

    猜你喜欢
    • 2019-02-11
    • 2018-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-21
    • 1970-01-01
    • 2015-05-10
    • 2019-09-27
    相关资源
    最近更新 更多