【发布时间】:2023-03-28 03:14:01
【问题描述】:
我一直在玩在 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 之间的所有对象?我是否需要更新我的文档以便以特定格式存储日期?
【问题讨论】:
-
你不说要查询哪个字段?
-
糟糕,我想查询 created_at 并查找两个日期之间的所有日期。
-
我很好奇为什么不使用时间戳,使用 Date Obj 有什么好处?
-
@Leo Date 对象的最大优势是自纪元以来的毫秒数或任何人类可读性。在这种情况下,将您的开始范围设置为
2010-04-29T00:00:00.000Z比以毫秒为单位计算相同的日期/时间要容易得多。您还可以很容易地进行时区转换。此外,Dates 已经处理了诸如闰日、闰秒和其他您通常不想自己处理的怪事。
标签: mongodb datetime twitter mongodb-query