开发者

MongoDB ruby dates

开发者 https://www.devze.com 2022-12-27 00:51 出处:网络
I have a 开发者_如何学Gocollection with an index on :created_at (which in this particular case should be a date)

I have a 开发者_如何学Gocollection with an index on :created_at (which in this particular case should be a date)

From rails what is the proper way to save an entry and then retrieve it by the date?

I'm trying something like:

Model: field :created_at, :type => Time

script:

Col.create(:created_at => Time.parse(another_model.created_at).to_s

and

Col.find(:all, :conditions => { :created_at => Time.parse(same thing) })

and it's not returning anything


The Mongo driver and various ORMs handle Date, Time and DateTime objects just fine; there's no reason to cast them to strings.

Col.create(:created_at => another_model.created_at)

And finding:

Col.all(:created_at => another_model.created_at)

You don't want to be setting strings, because dates are stored internally as BSON Date objects, and are indexed and searched as such. If you save them as strings, you won't be able to do things like greater than/less than/range comparisons effectively.


Col.create(:created_at => Time.parse(another_model.created_at).to_s)

That line would pass your time object as a String, take off the to_s to send it to the type parsing layer in your ORM (MongoMapper or Mongoid) as a Time object. That's the only error I can see that would cause it to not work.

0

精彩评论

暂无评论...
验证码 换一张
取 消