0

I have a collection in mongodb that contains a number of documents each of which contain a date field named thisdate - like this:

{"_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}

Using ruby and the mongo ruby gem I am trying to return a subset of the above documents based on a date range as follows.

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}

When I run this the documents returned are not within the date range I've requested. I was expecting to get documents where the thisdate is $gt than 2019-08-12 and $lt than 2019-09-06 but as you can see I am getting documents where the date is $gt than 2019-09-06.

Can someone please tell what I am doing wrong.

I'm using ruby v2.5.1 mongo gem v2.8.0 mongodb v3.6.3

Viktor
  • 2,623
  • 3
  • 19
  • 28
user3218909
  • 1
  • 1
  • 3

1 Answers1

0

The problem could be the way MongoDB (Ruby driver) handle Date.

Have a look at this issue on Github.

If I got it right you should use UTC Time instance, not a Date object.

It may be useful to use #to_mongo from Date#to_mongo-instance_method.

Ps:

I did some research and it seems that the problem should be the way Date objects are deserialized in Ruby drivers. see Mongo Documentation: Special Ruby Date Classes.

You can find an example on how to query a collection using Time.utc in this answer on StackOverflow.