我試圖執行一個MongoDB 聚合使用PHP查詢。MongoDB聚合與PHP
基本上,我得到了波紋管查詢,並將其使用MongoDB的shell時沒有問題,執行:
db.getCollection('frete').aggregate([
{$match : {
'$and': [
{ 'data_UTC': {$gte: ISODate('2017-01-20T00:00:00-02:00'), $lt: ISODate('2017-01-29T00:00:00-02:00') } },
{ 'carga': { $regex : new RegExp('soja', "i") }}
]
}
},
{$group : {_id: { $dateToString: { format: "%Y-%m-%d", date: { $subtract: ['$data_UTC', 1000 * 60 * 60 * 2] } } }, qtd_acessos: {$sum:1} }},
{ $sort: { _id: -1 } },
{ $limit: 50}
])
然而,當我試圖通過PHP執行相同的查詢(我composer.json
使用"mongodb/mongodb": "^1.0.0"
依賴)在調用MongoDB PHP's aggregate()
方法後,沒有任何響應返回:
有人可以幫我翻譯上面的查詢到它的PHP等價物嗎?我有以下,但也許我錯過了一些東西:
$start = new \MongoDB\BSON\UTCDateTime(strtotime('2017-01-20T00:00:00-02:00'));
$end = new \MongoDB\BSON\UTCDateTime(strtotime('2017-01-29T00:00:00-02:00'));
$regex = new \MongoDB\BSON\Regex('soja', 'i');
$filter = [
['$match' => [
'$and' => [
['data_UTC' => ['$gte' => $start, '$lt' => $end]],
['carga' => ['$regex' => $regex]]
]
]
],
['$group' => ['_id' => ['$dateToString' => ['format' => "%Y-%m-%d", 'date' => ['$subtract' => ['$data_UTC', 1000 * 60 * 60 * 2]]]], 'qtd_acessos' => ['$sum' => 1]]],
['$sort' => ['_id' => -1]],
['$limit' => 50]
];
謝謝!