使用PHP驱动程序的MongoDB组查询
我正在尝试使用官方PHP驱动程序和group()函数将工作的使用PHP驱动程序的MongoDB组查询,php,mongodb,group-by,Php,Mongodb,Group By,我正在尝试使用官方PHP驱动程序和group()函数将工作的MongoDB组查询转换为PHP等效查询 以下是正在工作的原始MongoDB查询: db.collection.group( { keyf: function(doc) { var date = new Date(doc.executed); var dateKey = (date.getMonth() + 1) + "/" + date.getDate() + "/" + date.getFul
MongoDB
组查询转换为PHP
等效查询
以下是正在工作的原始MongoDB查询:
db.collection.group(
{
keyf: function(doc) {
var date = new Date(doc.executed);
var dateKey = (date.getMonth() + 1) + "/" + date.getDate() + "/" + date.getFullYear();
return {'day': dateKey};
},
cond: { executed: { $gt: new Date('01/01/2013') }},
initial: { executions:0 },
reduce: function(obj, prev) { prev.executions++; }
});
这是我的不工作的PHP代码:
$keyf = new MongoCode('function(doc) {
var date = new Date(doc.executed);
var dateKey = (date.getMonth() + 1) + "/" + date.getDate() + "/" + date.getFullYear();
return {\'day\': dateKey};
}');
$initial = array("executions" => 0);
$reduce = "function(obj, prev) { prev.executions++; }";
$conditionals = array("executed" => array('$gt' => "new Date('01/01/2013')"));
$result = $c->group($keyf, $initial, $reduce, array("condition" => $conditionals));
以及$result
的print\u r()
:
Array
(
[retval] => Array
(
)
[count] => 0
[keys] => 0
[ok] => 1
)
谢谢 您的
条件
是比较字符串而不是日期。你需要一个新的。比如:
$cond_date = new MongoDate(strtotime("2013-01-01 00:00:00"));
$conditionals = array("executed" => array('$gt' => $cond_date));
完美的无论如何,我可以反转排序,而不是返回
1/30/2013
到1/1/2013
,我希望1/1/2013
到1/30/2013
。谢谢