查找两个日期之间的对象 MongoDB
我一直在尝试在 mongodb 中存储推文,每个对象如下所示:
{
"_id" : ObjectId("4c02c58de500fe1be1000005"),
"contributors" : null,
"text" : "Hello world",
"user" : {
"following" : null,
"followers_count" : 5,
"utc_offset" : null,
"location" : "",
"profile_text_color" : "000000",
"friends_count" : 11,
"profile_link_color" : "0000ff",
"verified" : false,
"protected" : false,
"url" : null,
"contributors_enabled" : false,
"created_at" : "Sun May 30 18:47:06 +0000 2010",
"geo_enabled" : false,
"profile_sidebar_border_color" : "87bc44",
"statuses_count" : 13,
"favourites_count" : 0,
"description" : "",
"notifications" : null,
"profile_background_tile" : false,
"lang" : "en",
"id" : 149978111,
"time_zone" : null,
"profile_sidebar_fill_color" : "e0ff92"
},
"geo" : null,
"coordinates" : null,
"in_reply_to_user_id" : 149183152,
"place" : null,
"created_at" : "Sun May 30 20:07:35 +0000 2010",
"source" : "web",
"in_reply_to_status_id" : {
"floatApprox" : 15061797850
},
"truncated" : false,
"favorited" : false,
"id" : {
"floatApprox" : 15061838001
}
我如何编写一个查询来检查 created_at 并查找 18:47 到 19:00 之间的所有对象?我是否需要更新文档以便以特定格式存储日期?
I've been playing around storing tweets inside mongodb, each object looks like this:
{
"_id" : ObjectId("4c02c58de500fe1be1000005"),
"contributors" : null,
"text" : "Hello world",
"user" : {
"following" : null,
"followers_count" : 5,
"utc_offset" : null,
"location" : "",
"profile_text_color" : "000000",
"friends_count" : 11,
"profile_link_color" : "0000ff",
"verified" : false,
"protected" : false,
"url" : null,
"contributors_enabled" : false,
"created_at" : "Sun May 30 18:47:06 +0000 2010",
"geo_enabled" : false,
"profile_sidebar_border_color" : "87bc44",
"statuses_count" : 13,
"favourites_count" : 0,
"description" : "",
"notifications" : null,
"profile_background_tile" : false,
"lang" : "en",
"id" : 149978111,
"time_zone" : null,
"profile_sidebar_fill_color" : "e0ff92"
},
"geo" : null,
"coordinates" : null,
"in_reply_to_user_id" : 149183152,
"place" : null,
"created_at" : "Sun May 30 20:07:35 +0000 2010",
"source" : "web",
"in_reply_to_status_id" : {
"floatApprox" : 15061797850
},
"truncated" : false,
"favorited" : false,
"id" : {
"floatApprox" : 15061838001
}
How would I write a query which checks the created_at and finds all objects between 18:47 and 19:00? Do I need to update my documents so the dates are stored in a specific format?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
查询日期范围(特定月份或日期)MongoDB Cookbook对此事有很好的解释,但以下是我尝试过的内容我自己出来了,它似乎有效。根据我的实验,您需要将日期序列化为 MongoDB 支持的格式,因为以下给出了不需要的搜索结果。
在第二个示例中,预计不会有结果,但仍然得到了结果。这是因为已经完成了基本的字符串比较。
Querying for a Date Range (Specific Month or Day)in theMongoDB Cookbookhas a very good explanation on the matter, but below is something I tried out myself and it seems to work.Based on my experiments you will need to serialize your dates into a format that MongoDB supports, because the following gave undesired search results.
In the second example no results were expected, but there was still one gotten. This is because a basic string comparison is done.
澄清一下。重要的是要知道:
这是一个工作代码片段,我们做了一些日期操作,以确保 Mongo (这里我使用 mongoose 模块,并希望日期属性小于(之前)作为 myDate 参数给出的日期的行的结果)可以正确处理它:
To clarify. What is important to know is that:
Here is a working snippet of code, where we do a little bit of date manipulation to ensure Mongo (here i am using mongoose module and want results for rows whose date attribute is less than (before) the date given as myDate param) can handle it correctly:
Python 和 pymongo
使用集合
posts
中的pymongo
在 Python 中查找两个日期之间的对象(基于 教程):其中
{"$gte": from_date, "$lt": to_date}< /code> 以
datetime.datetime
类型指定范围。Python and
pymongo
Finding objects between two dates in Python with
pymongo
in collectionposts
(based on the tutorial):Where
{"$gte": from_date, "$lt": to_date}
specifies the range in terms ofdatetime.datetime
types.将
collection
替换为要执行查询的集合名称Replace
collection
with name of collection you want to execute queryMongoDB 实际上将日期的毫秒存储为 int(64),如 http://bsonspec.org/# 所规定的但是
,当您检索日期时,它可能会变得非常混乱,因为客户端驱动程序将使用自己的本地时区实例化日期对象。 mongo 控制台中的 JavaScript 驱动程序肯定会执行此操作。
因此,如果您关心时区,请确保您知道取回时区时应该是什么。这对于查询来说应该不重要,因为它仍然等于相同的 int(64),无论您的日期对象位于哪个时区(我希望)。但我肯定会使用实际日期对象(而不是字符串)进行查询,并让驱动程序执行其操作。
MongoDB actually stores the millis of a date as an int(64), as prescribed by http://bsonspec.org/#/specification
However, it can get pretty confusing when you retrieve dates as the client driver will instantiate a date object with its own local timezone. The JavaScript driver in the mongo console will certainly do this.
So, if you care about your timezones, then make sure you know what it's supposed to be when you get it back. This shouldn't matter so much for the queries, as it will still equate to the same int(64), regardless of what timezone your date object is in (I hope). But I'd definitely make queries with actual date objects (not strings) and let the driver do its thing.
与 Moment.js 和 比较查询运算符
Using with Moment.js and Comparison Query Operators
使用以下代码通过
$gte
和$lt
查找两个日期之间的记录:Use this code to find the record between two dates using
$gte
and$lt
:将created_at 日期保存为ISO 日期格式,然后使用$gte 和$lte。
Save created_at date in ISO Date Format then use $gte and $lte.
您也可以检查一下。如果您使用此方法,请使用 parse 函数从 Mongo 数据库获取值:
You can also check this out. If you are using this method, then use the parse function to get values from Mongo Database:
使用 $gte 和 $lte 在 mongodb 中查找日期数据
use $gte and $lte to find between date data's in mongodb
对于使用 Make(以前称为 Integromat)和 MongoDB 的用户:
我正在努力寻找查询两个日期之间所有记录的正确方法。最后,我所要做的就是按照此处一些解决方案的建议删除
ISODate
。所以完整的代码是:
这个 文章帮助我实现了目标。
更新
在 Make(以前称为 Integromat)中实现上述代码的另一种方法是使用 parseDate 函数。因此下面的代码将返回与上面相同的结果:
⚠️ 请务必在引号之间包裹
{{parseDate("2017-01-01"; "YYYY-MM-DD")}}
标记。For those using Make (formerly Integromat) and MongoDB:
I was struggling to find the right way to query all records between two dates. In the end, all I had to do was to remove
ISODate
as suggested in some of the solutions here.So the full code would be:
This article helped me achieve my goal.
UPDATE
Another way to achieve the above code in Make (formerly Integromat) would be to use the parseDate function. So the code below will return the same result as the one above :
⚠️ Be sure to wrap
{{parseDate("2017-01-01"; "YYYY-MM-DD")}}
between quotation marks.当您将日期填入 Mongo 时,请将其转换为 GMT 时区。这样就永远不会出现时区问题。然后,当您将数据拉回来进行演示时,只需在 twitter/timezone 字段上进行数学计算即可。
Convert your dates to GMT timezone as you're stuffing them into Mongo. That way there's never a timezone issue. Then just do the math on the twitter/timezone field when you pull the data back out for presentation.
为什么不将字符串转换为 YYYYMMDDHHMMSS 形式的整数?每次时间增量都会创建一个更大的整数,您可以对整数进行过滤,而不必担心转换为 ISO 时间。
Why not convert the string to an integer of the form YYYYMMDDHHMMSS? Each increment of time would then create a larger integer, and you can filter on the integers instead of worrying about converting to ISO time.
斯卡拉:
使用 joda DateTime 和 BSON 语法 (reactivemongo):
其中“2021-09-08T06:42:51.697Z”的
millisOfDay().withMinimumValue()
将是“2021-09-08T00:00:00.000” Z"和
其中 millisOfDay()。 withMaximumValue() 对于“2021-09-08T06:42:51.697Z”将是“2021-09-08T23:59:99.999Z”
Scala:
With joda DateTime and BSON syntax (reactivemongo):
where
millisOfDay().withMinimumValue()
for "2021-09-08T06:42:51.697Z" will be "2021-09-08T00:00:00.000Z"and
where
millisOfDay(). withMaximumValue()
for "2021-09-08T06:42:51.697Z" will be "2021-09-08T23:59:99.999Z"我根据我的要求在这个模型中尝试过,我需要在以后创建对象时存储一个日期,我想检索两个日期之间的所有记录(文档)
在我的 html 文件中
我在我的 py (python) 文件中使用以下格式 mm/dd/yyyy
我将其转换为“iso fomate”
以以下方式
保存在我的 dbmongo 集合中,并使用“SelectedDate”作为我的集合中的字段,
以检索我使用以下查询的 2 个日期之间的数据或文档
i tried in this model as per my requirements i need to store a date when ever a object is created later i want to retrieve all the records (documents ) between two dates
in my html file
i was using the following format mm/dd/yyyy
in my py (python) file i converted it into "iso fomate"
in following way
and saved in my dbmongo collection with "SelectedDate" as field in my collection
to retrieve data or documents between to 2 dates i used following query