如何使用Laravel中的Jessengers包在mongodb聚合函数中使用where/whereDate?
如何在聚合mongodb查询中使用where/whereDate 我在laravel中使用聚合查询并按字段分组 这里的问题是,我需要添加一个条件来获取特定日期的分组数据,即如何使用Laravel中的Jessengers包在mongodb聚合函数中使用where/whereDate?,mongodb,laravel,mongodb-query,Mongodb,Laravel,Mongodb Query,如何在聚合mongodb查询中使用where/whereDate 我在laravel中使用聚合查询并按字段分组 这里的问题是,我需要添加一个条件来获取特定日期的分组数据,即created\u at 如果我不使用pass$date即$date=null,则以下代码工作正常: public static function getUsersByTypeLogin($date = null) { $q = self::raw()->aggregate(
created\u at
如果我不使用pass$date
即$date=null
,则以下代码工作正常:
public static function getUsersByTypeLogin($date = null)
{
$q = self::raw()->aggregate(
[
array(
'$match' => array(
'type' => self::LOGIN,
)
),
array(
'$group' => array(
'_id' => array(
'lat' => '$latitude',
'long' => '$longitude',
'country' => '$country',
'state' => '$state'
),
'count' => array('$sum' => 1)
)
)
]
);
if(true == $date){
$q = $q->whereDate('created_at','=',$date);
}
return $q;
}
但是如果我传递$date
它会返回一个错误,它会说:
消息:“调用未定义的方法MongoDB\Driver\Cursor::whereDate()”
$date是碳元素对象,例如:
Carbon @1549843200 {#791
date: 2019-02-11 00:00:00.0 +00:00
}
请在这方面帮助我。试试这个
$q = self::raw(function($collection) use($date) {
return $collection->aggregate([
array(
'$match' => array(
'type' => self::LOGIN,
'created_date' => $date,
)
),
array(
'$group' => array(
'_id' => array(
'lat' => '$latitude',
'long' => '$longitude',
'country' => '$country',
'state' => '$state'
),
'count' => array('$sum' => 1)
)
)
]);
});
@DineshSuthar更新了我的答案,你能试试吗?如果出现任何错误,请告诉我如果
$date
passed为空怎么办?我想使用条件,例如,如果日期已过,则与在处创建的匹配,否则不匹配。@DineshSuthar如果日期已过,此解决方案对您有效?否,我得到的是空数组:1)Carbon date对象:Carbon@1549843200{#791日期:2019-02-11 00:00:00.0+00:00}2)结果数组:[]删除条件下创建的_并显示集合->聚合转储结果我想检查它们返回的内容?