简体   繁体   English

以UTC格式日期为过滤器查询mongodb

[英]Query mongodb with UTC format date as filter

I have 1 million+ record in MongoDB database and need to query records, such that their date field contains a value which is not in the last 30 days.我在 MongoDB 数据库中有 100 万条以上的记录,需要查询记录,以便它们的日期字段包含一个不是最近 30 天的值。 The problem is it's present in UTCString() format and comparison in this format is not working.问题是它以 UTCString() 格式存在,并且这种格式的比较不起作用。

Date in database looks like this - "Tue, 06 Apr 2021 23:22:43 GMT"数据库中的日期如下所示 - “2021 年 4 月 6 日星期二 23:22:43 GMT”

instead of querying using the UTC date, use can use the Unix version of the date eg.而不是使用 UTC 日期查询,使用可以使用日期的 Unix 版本,例如。 use 1616284800000 instead of 2021-03-21T00:00:000Z使用 1616284800000 而不是 2021-03-21T00:00:000Z

So your query will look like this所以您的查询将如下所示

db.Collection.find({"dateField":{$lte:new Date(1616284800000)}}) db.Collection.find({"dateField":{$lte:new Date(1616284800000)}})

Also if you haven't already tried this you can另外,如果您还没有尝试过,您可以

db.Collection.find({"dateField":{$lte:ISODate("2021-03-21T00:00:00.000Z")}}) db.Collection.find({"dateField":{$lte:ISODate("2021-03-21T00:00:00.000Z")}})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM