MongoDB仅比较日期而没有时间 [英] MongoDB comparing dates only without times

查看:217
本文介绍了MongoDB仅比较日期而没有时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在MongoDB中查询仅包含日期而非日期的日期字段?例如,如果MongoDB将2015年7月7日存储为 any 时间,那么我只想将该天与今天的日期进行匹配.

How do I query for a date field in MongoDB with the date only, and not times? For example, if MongoDB stores July 7, 2015 with any time, I want to match that day only with today's date.

在MySQL中,我会做SELECT col1, col2 FROM table WHERE DATE(date_field) = DATE(NOW());

In MySQL, I would do SELECT col1, col2 FROM table WHERE DATE(date_field) = DATE(NOW());

请注意,MySQL如何具有仅在匹配期间将 date_field 更改为日期的功能.匹配期间,我可以在MongoDB中使用类似功能执行此操作吗?像Collection.find({ dateonly(datetimefield): dateonly(new Date() });之类的东西?

Notice how MySQL has a function to change the date_field to the date only during matching. Can I do this in MongoDB with a similar function during matching? Like Collection.find({ dateonly(datetimefield): dateonly(new Date() }); or something of the like?

我了解Mongo在GMT中存储日期/时间,所以我还必须在仅匹配日期之前将存储的GMT日期+时间从GMT转换为本地,但是要让我开始,想知道我是否可以剥离日期匹配的时间.例如,如果格林尼治标准时间现在是7月8日03:00,而美国东部时间是7月7日23:00,那么我想匹配7月7日.

I understand Mongo stores date/times in GMT, so I would also have to convert the stored GMT date+time from GMT to Local before matching the date only, but to get me started, would like to know if I can strip the time off of a date match. For example, if GMT now is July 8 at 03:00 but Local Eastern Time US is July 7 at 23:00 then I would want to match July 7.

推荐答案

更新于2018-06-26,修复了代码以使用moment()代替新的Date()

所以听起来好像没有mongodb等同于MySQL的DATE函数,例如SELECT col1, col2 FROM table WHERE DATE(date_field) = DATE(NOW())

So it sounds like there is no mongodb equivalent to MySQL's DATE function, like with SELECT col1, col2 FROM table WHERE DATE(date_field) = DATE(NOW())

我用MomentJS时区( http://momentjs.com/timezone /)将日期/时间转换为仅日期的数字字段,然后将日期存储为数字.

In lieu of a native solution, I solved this by use MomentJS Timezone (http://momentjs.com/timezone/) to convert the date/time to a date-only numerical field, then I store the date as a number.

在我的JavaScript代码中(在MongoDB外部):

In my javascript code (outside of MongoDB):

var localDateOnly = function(timezone, d) {
  if (d == undefined) { d = new Date(); } // current date/time
  return Number( moment(d).tz(timezone).format("YYYYMMDD") );
}

然后,我将仅日期字段存储在Mongo记录中.

Then I store a date-only field in the Mongo record.

var myDate = localDateOnly("America/New_York");  // today, in YYYYMMDD number
db.birthdays.insert(
  { dateonly: myDate, event: "This day is my birthday!" }
);

然后,在我的Javascript代码中,我可以轻松地查询今天,明天,特定日期等.

Then in my Javascript code, I can easily query today, tomorrow, specific days, etc.

// today
var myDate = localDateOnly("America/New_York");
db.birthdays.find( { dateonly: myDate } );

// tomorrow
var myDate = localDateOnly(
  "America/New_York",
  moment().add( 1, "days" )
);  // tomorrow
db.birthdays.find( { dateonly: myDate } );

// someone wants to know birthdays on the calendar on July 15, 2015
// regardless which time zone they are in
// just find the date in YYYYMMDD format
db.birthdays.find( { dateonly: 20150715 } );

希望这对某人有帮助.它存储数字而不是字符串,目的是提高其索引的性能.在将其存储到要检查的数据库中之前,我还有其他代码强制使用看起来有效的数字:

Hope this helps someone. It's storing a number, not a string, on purpose to improve performance on its index. I have other code which enforces a valid-looking number, before storing into the database it checks:

moment( 20150715, "YYYYMMDD", true ).isValid()  // returns true & allowed to store in the database
moment( 150715, "YYYYMMDD", true ).isValid()  // returns false, don't insert into db

这篇关于MongoDB仅比较日期而没有时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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