我正在编写一个查询来搜索在特定日期创建的用户,例如
$date = '2018-05-02 15:46:41.000Z';
User::raw(function ($col) use($date) {
return $col->aggregate([
['$match' =>
'created_at' => ['$gte' => new \MongoDB\BSON\UTCDateTime(strtotime($date))]
]
]);
});
给出样本数据,如
{
"_id" : ObjectId("5ae9dd61ef7a5754f159c656"),
"name" : "Effie Larkin",
"created_at" : ISODate("2018-05-02 15:46:41.000Z"),
}...
预期结果应为
{
"users" : [
"_id" : {"$oid": "5ae9dd61ef7a5754f159c656"},
"name" "Effie Larkin",
"created_at" : "2018-05-02 15:46:41"
]
}...
但我得到一个空的json数组[]
。我认为问题出在UTCDateTime函数中,但我不知道如何将dateTime传递给它。我希望通过一个星期前或者昨天等时间间隔...如果有人能帮助我将非常感谢,谢谢
更新,下面的示例修复了它
$date = '2018-05-02';
User::raw(function ($col) use($date) {
return $col->aggregate([
['$match' =>
'created_at' => ['$gte' => new \MongoDB\BSON\UTCDateTime(strtotime($date)*1000)]
]
]);
});