MongoDB/Mongoose 在特定日期查询? [英] MongoDB/Mongoose querying at a specific date?

查看:41
本文介绍了MongoDB/Mongoose 在特定日期查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以查询特定日期?

Is it possible to query for a specific date ?

我在 mongo Cookbook 中发现我们可以为一个范围做它查询一个日期范围像这样:

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).

您保存的日期可能是 new Date(),其中包括时间组件.要查询这些时间,您需要创建一个包含一天中所有时刻的日期范围.

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)
    }
})

这篇关于MongoDB/Mongoose 在特定日期查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆