我在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
的文档。
有人可以告诉我我做错了吗。
我正在使用 红宝石v2.5.1 mongo gem v2.8.0 mongodb v3.6.3
答案 0 :(得分:0)
问题可能出在MongoDB(Ruby驱动程序)处理日期的方式上。
如果我做对了,则应使用UTC时间实例,而不要使用Date对象。
使用Date#to_mongo-instance_method中的#to_mongo
可能会有用。
Ps:
我做了一些研究,看来问题应该出在Ruby驱动程序中将Date对象反序列化的方式。 请参阅Mongo文档:Special Ruby Date Classes。
您可以在此StackOverflow的answer中找到有关如何使用Time.utc
查询集合的示例。