簡體   English   中英

如何根據紅寶石中的日期范圍在mongo集合中查找文檔

[英]How do I find documents in a mongo collection based on a date range in ruby

我在mongodb中有一個集合,其中包含許多文檔,每個文檔都包含一個名為thisdate的日期字段-像這樣:

{"_id"=>BSON::ObjectId('5d79495d5c3ce60e83b11764'), "blah"=>"some text", "thisdate"=>2019-09-11 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d7949785c3ce60e96502476'), "blah"=>"some text", "thisdate"=>2019-09-11 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d7949b95c3ce60eaa3b1ff2'), "blah"=>"some text", "thisdate"=>2019-09-11 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d7949c75c3ce60ebb3419e7'), "blah"=>"some text", "thisdate"=>2019-09-11 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d794a1a5c3ce611c9f5f0fe'), "blah"=>"some text", "thisdate"=>2019-09-06 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d794a1c5c3ce611d92f3844'), "blah"=>"some text", "thisdate"=>2019-09-06 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d794a1e5c3ce611e9a8bcab'), "blah"=>"some text", "thisdate"=>2019-09-06 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d794a1f5c3ce611f986611e'), "blah"=>"some text", "thisdate"=>2019-09-06 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d794a245c3ce6120a84876a'), "blah"=>"some text", "thisdate"=>2019-09-06 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d794a265c3ce6121aa462a8'), "blah"=>"some text", "thisdate"=>2019-09-06 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d794a295c3ce6122b2cc00a'), "blah"=>"some text", "thisdate"=>2019-09-05 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d794a2b5c3ce6123ce16418'), "blah"=>"some text", "thisdate"=>2019-09-05 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d794a2f5c3ce6124d52266f'), "blah"=>"some text", "thisdate"=>2019-09-01 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d794a455c3ce6126839621b'), "blah"=>"some text", "thisdate"=>2019-09-01 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d794a455c3ce6126839621c'), "blah"=>"some text", "thisdate"=>2019-09-01 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d794a455c3ce6126839621d'), "blah"=>"some text", "thisdate"=>2019-09-01 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d794a455c3ce6126839621e'), "blah"=>"some text", "thisdate"=>2019-09-01 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d794a455c3ce6126839621f'), "blah"=>"some text", "thisdate"=>2019-09-01 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d794a475c3ce61278d8d92d'), "blah"=>"some text", "thisdate"=>2019-09-01 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d794a475c3ce61278d8d92e'), "blah"=>"some text", "thisdate"=>2019-09-01 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d794a475c3ce61278d8d92f'), "blah"=>"some text", "thisdate"=>2019-09-01 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d794a475c3ce61278d8d930'), "blah"=>"some text", "thisdate"=>2019-09-01 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d794a475c3ce61278d8d931'), "blah"=>"some text", "thisdate"=>2019-09-01 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d794a505c3ce6128956ef46'), "blah"=>"some text", "thisdate"=>2019-08-22 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d794a505c3ce6128956ef47'), "blah"=>"some text", "thisdate"=>2019-08-22 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d794a505c3ce6128956ef48'), "blah"=>"some text", "thisdate"=>2019-08-22 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d794a505c3ce6128956ef49'), "blah"=>"some text", "thisdate"=>2019-08-22 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d794a505c3ce6128956ef4a'), "blah"=>"some text", "thisdate"=>2019-08-22 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d794a555c3ce6129b7fa3cc'), "blah"=>"some text", "thisdate"=>2019-08-17 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d794a555c3ce6129b7fa3cd'), "blah"=>"some text", "thisdate"=>2019-08-17 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d794a555c3ce6129b7fa3ce'), "blah"=>"some text", "thisdate"=>2019-08-17 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d794a555c3ce6129b7fa3cf'), "blah"=>"some text", "thisdate"=>2019-08-17 00:00:00 UTC}
{"_id"=>BSON::ObjectId('5d794a555c3ce6129b7fa3d0'), "blah"=>"some text", "thisdate"=>2019-08-17 00:00:00 UTC}

我試圖使用ruby和mongo ruby​​ gem,根據日期范圍返回上述文檔的子集,如下所示。

puts "date range"

from = Date.today-30 # 2019-08-12
puts "from #{from}" # 2019-09-06

to = Date.today-5
puts "to #{to}"

dbclient['testing'].find("$and": ["thisdate": {"$lt": to},"thisdate": {"$gt": from}]).each {|this| puts this}

運行此文件時,返回的文件不在我要求的日期范圍內。 我希望獲得thisdate$gt than 2019-08-12 and $lt than 2019-09-06但是如您所見,我正在獲取日期是$gt than 2019-09-06

有人可以告訴我我在做什么錯。

我正在使用ruby v2.5.1 mongo gem v2.8.0 mongodb v3.6.3

問題可能出在MongoDB(Ruby驅動程序)處理日期的方式上。

在Github上看看這個問題

如果我做對了,則應使用UTC Time實例,而不要使用Date對象。

使用Date#to_mongo-instance_method中的 #to_mongo可能會有用。

附:

我做了一些研究,看來問題應該出在Ruby驅動程序中將Date對象反序列化的方式。 請參閱Mongo文檔: 特殊Ruby日期類

您可以在StackOverflow的此答案中找到有關如何使用Time.utc查詢集合的示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM