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

查看:460
本文介绍了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 day, month, year).

可能的是,您保存的日期是 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天全站免登陆