如何在Ruby on Rails中将时间戳字符串转换为日期并存储到MongoDB中?
- 内容介绍
- 文章标签
- 相关推荐
本文共计259个文字,预计阅读时间需要2分钟。
接受时间作为用户的字符串,例如上午10:00,或如需,接受2015-08-27 10:00 am。需要使用rails的Date函数(Date)2015-08-27 10:00 am来转换字符串日期,然后保存在d中。
我接受时间作为用户的字符串,如“上午10:00”,或者如果需要,我可以接受来自用户的“2015-08-27 10:00 am”.需要使用rails函数Date(“2015-08-27 10:00 am”)来转换字符串的日期然后需要保存在db?
我正在使用
>ruby2.2.2
>铁轨4.1.4
> mongoid 4.0.0
time = Time.parse '2015-08-27 10:00am'
Mongoid
如果您使用Mongoid,create your model并将其分配给相应的字段.例如:
user = User.create(username: 'dimakura', talk_time: time)
Ruby驱动程序
如果您使用Ruby Driver,请使用以下代码:
client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'mydb') result = client[:users].insert_one({ username: 'dimakura', talk_time: time })
本文共计259个文字,预计阅读时间需要2分钟。
接受时间作为用户的字符串,例如上午10:00,或如需,接受2015-08-27 10:00 am。需要使用rails的Date函数(Date)2015-08-27 10:00 am来转换字符串日期,然后保存在d中。
我接受时间作为用户的字符串,如“上午10:00”,或者如果需要,我可以接受来自用户的“2015-08-27 10:00 am”.需要使用rails函数Date(“2015-08-27 10:00 am”)来转换字符串的日期然后需要保存在db?
我正在使用
>ruby2.2.2
>铁轨4.1.4
> mongoid 4.0.0
time = Time.parse '2015-08-27 10:00am'
Mongoid
如果您使用Mongoid,create your model并将其分配给相应的字段.例如:
user = User.create(username: 'dimakura', talk_time: time)
Ruby驱动程序
如果您使用Ruby Driver,请使用以下代码:
client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'mydb') result = client[:users].insert_one({ username: 'dimakura', talk_time: time })

