'Rails/MongoDB: How to query mongodb model with string to date typecast?

I have a rails mongoid model with a string field end_date as follows

class Project
  include Mongoid::Document
  include Mongoid::Attributes::Dynamic
  include Mongoid::Timestamps

  field :end_date, type: String
end

end_date is having values stored as September 2021, January 2025 etc. Now How can I find all the projects which has end_date less than current month?



Solution 1:[1]

If i am understanding correctly you want to compare date . You can use the following documentation to fit your purpose -

https://api.rubyonrails.org/v4.2.5/classes/Date.html

Get the date in a controller method and then convert before comparing with the current date . Hopefully this answers the question .

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Caffeine Coder