Is it possible to query for a specific date ?

I found in the mongo Cookbook that we can do it for a range Querying for a Date Range Like that :

db.posts.find({"created_on": {"$gte": start, "$lt": end}})

But is it possible for a specific date ? This doesn't work :

db.posts.find({"created_on": new Date(2012, 7, 14) })

推荐答案

That should work if the dates you saved in the DB are without time (just year, month, day).

Chances are that the dates you saved were new Date(), which includes the time components. To query those times you need to create a date range that includes all moments in a day.

db.posts.find({ //query today up to tonight
    created_on: {
        $gte: new Date(2012, 7, 14), 
        $lt: new Date(2012, 7, 15)
    }
})

Node.js相关问答推荐

express返回意外的URL

获取驱动器文件夹的直接子文件夹时出现问题

如何在Mongoose for MongoDB中编写此查询

在函数上执行 toString 的Typescript 会产生奇怪的字符 (path_1, (0, Promise.writeFile))

无法从 mongoDB 访问数据?

当API返回400状态代码时,使用Reactjs fetch获取错误消息

使用 NPM 8 安装本地构建

如何使用 node.js 将两个或多个 API 请求组合成一个端点并表达

使用 fs.createWriteStream 将数据写入 bigquery (node.js) 时出现模式错误

Typescript typeRoots 未检测到类型定义

如何在 NestJS 中使用外部生成的 swagger.json?

如果 express.js (node.js) http 请求在完成之前关闭会发生什么?

Aptana Studio 是否有 NodeJS 插件?

分块 WebSocket 传输

容器之间的 Docker HTTP 请求

如何在 NodeJs 中下载和解压缩内存中的 zip 文件?

为什么我们要为 Angular 2.0 安装 Node.js?

按日期时间字段获取最新的 MongoDB 记录

Node.js - 判断是否安装了模块而不实际需要它

响应分块时获取整个响应正文?