Ben
Ben

Reputation: 1368

How to Query Document Created Today Using MongoDB?

I'm using Mongoid for my simple Rails 3.x app. I have this model:

class Report

  include Mongoid::Document
  include Mongoid::Timestamps

end

I want to get all Report created today using my current timezone which is +08:00. I tried using this snippet:

Report.where(:created_at => DateTime.now.at_beginning_of_day.utc..Time.now.utc).to_a

However, when I trigger:

DateTime.now.at_beginning_of_day.utc

It gets the date yesterday and the timezone is +00:00

Upvotes: 3

Views: 1397

Answers (1)

Baruch
Baruch

Reputation: 1133

You should probably be calling Report.where(:created_at.gt => ... to get all reports created since the beginning of the day.

Upvotes: 1

Related Questions