Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-1578

DateTimes in $or queries

    • Type: Icon: Task Task
    • Resolution: Done
    • 2.4.1
    • Affects Version/s: None
    • Component/s: None

      I'm getting an error querying using dates if the datetime is inside an $or clause:

      require 'mongoid'
      
      Mongoid.configure do |config|
        config.master = Mongo::Connection.new.db("orred_dates")
      end
      
      class Rockstar
        include Mongoid::Document
      
        has_many :gigs do
          def find_future
            where(time: {"$gt" => DateTime.now})
          end
          def find_future_or_now
            where(
              "$or" => [
                {time: {"$gt" => DateTime.now}},
                {right_now: true}
              ]
            )
          end
          def find_future_or_now_with_time
            where(
              "$or" => [
                {time: {"$gt" => Time.now}},
                {right_now: true}
              ]
            )
          end
        end
      
      end
      
      class Gig
        include Mongoid::Document
      
        belongs_to :rockstar
      
        field :time, type: DateTime
        field :right_now, type: Boolean, default: false
      end
      
      rockstar = Rockstar.new
      
      rockstar.gigs.build(time: DateTime.parse("May 25, 2012")).save
      rockstar.gigs.build(right_now: true).save
      rockstar.gigs.build(time: DateTime.parse("May 25, 1965")).save
      rockstar.save
      rockstar.reload
      
      puts rockstar.gigs.find_future          # works fine
      puts rockstar.gigs.find_future_or_now_with_time  #also works
      puts rockstar.gigs.find_future_or_now   # error
      

      The full trace is:

      /home/isaac/.rvm/gems/ruby-1.9.2-p290/gems/bson-1.5.2/lib/bson/bson_c.rb:24:in `serialize': DateTime is not currently supported; use a UTC Time instance instead. (BSON::InvalidDocument)
      	from /home/isaac/.rvm/gems/ruby-1.9.2-p290/gems/bson-1.5.2/lib/bson/bson_c.rb:24:in `serialize'
      	from /home/isaac/.rvm/gems/ruby-1.9.2-p290/gems/mongo-1.5.2/lib/mongo/cursor.rb:602:in `construct_query_message'
      	from /home/isaac/.rvm/gems/ruby-1.9.2-p290/gems/mongo-1.5.2/lib/mongo/cursor.rb:465:in `send_initial_query'
      	from /home/isaac/.rvm/gems/ruby-1.9.2-p290/gems/mongo-1.5.2/lib/mongo/cursor.rb:458:in `refresh'
      	from /home/isaac/.rvm/gems/ruby-1.9.2-p290/gems/mongo-1.5.2/lib/mongo/cursor.rb:128:in `next'
      	from /home/isaac/.rvm/gems/ruby-1.9.2-p290/gems/mongo-1.5.2/lib/mongo/cursor.rb:290:in `each'
      	from /home/isaac/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.4.0/lib/mongoid/cursor.rb:48:in `each'
      	from /home/isaac/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.4.0/lib/mongoid/contexts/mongo.rb:242:in `iterate'
      	from /home/isaac/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.4.0/lib/mongoid/criteria.rb:145:in `block in each'
      	from /home/isaac/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.4.0/lib/mongoid/criteria.rb:145:in `tap'
      	from /home/isaac/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.4.0/lib/mongoid/criteria.rb:145:in `each'
      	from mongoid_or_datetime.rb:44:in `to_a'
      	from mongoid_or_datetime.rb:44:in `puts'
      	from mongoid_or_datetime.rb:44:in `puts'
      	from mongoid_or_datetime.rb:44:in `<main>'
      

      The direct Mongo query I'd expect here works like it should:

      db.gigs.find({
        $or: [
          {right_now: true},
          {time : {$gt : ISODate("2012-01-10T04:00:00Z")}}
        ]
      })
      

      Am I doing something wrong?

            Assignee:
            Unassigned Unassigned
            Reporter:
            icambron Isaac Cambron
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: