php - Jenssegers MongoDB "like query"在 ISODate 上返回空白数组

标签 php mongodb laravel-4 jenssegers-mongodb

我有以下查询,但它返回一个空数组(我知道这个查询应该返回一条记录这一事实)

$created_at = date("Y-m");

$content = ContentModel::where('userId', $id->_id)
->where('created_at', 'like', "%{$created_at}%")
->orderBy('fav', 'DESC')
->get();

如果我删除 ->where('created_at', 'like', "%{$created_at}%") 它会返回一切正常,但我想要今年和本月的内容但是当我输入时查询不起作用。

数据库中的日期为ISODate格式 “创建时间”:ISODate("2015-02-03T16:29:26.965Z")

我猜这是因为 ISODate 格式。我如何获得我需要的结果?

谢谢

最佳答案

改用这段代码:

$created_at = new DateTime(date('F jS Y h:i:s A', strtotime('first day of ' . date('F Y'))));

$content = ContentModel::where('userId', $id->_id)
            ->where('created_at', '>', $created_at)
            ->orderBy('fav', 'DESC')
            ->get();

在此代码中,$created_at 变量表示当前月份的 1 号。

关于php - Jenssegers MongoDB "like query"在 ISODate 上返回空白数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28336538/

相关文章:

php - 拉拉维尔 : redirect to index view does not fire method

php - laravel 4 我在哪里可以找到测试用例?

php - 如何选择数据库中特定列具有值的所有行?

javascript - 有更好的方法来编写这段代码吗? -- 在 NodeJS 中使用 Promises 链接 HTTP 请求 - 使用 Await/Async?

mongodb - 如何使用 MongoDB 配置副本集

php - Laravel:在使用 Query Builder 或 Eloquent ORM 时对每次插入/更新执行一些任务

php - 用cron在PHP中编写文件

php - 忽略 mysql 搜索中的撇号

php - Doctrine :渴望加载懒惰关系的关系?

mongodb - 从 mongodb 聚合获取深度嵌套数组并包含在结果中