加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 存储时间戳为Number Mongoid

发布时间:2020-12-16 19:36:57 所属栏目:百科 来源:网络整理
导读:我是Mongoid的新手.在我的模型文件中,我创建了一个数据类型为BigDecimal的字段.我想在其中存储时间戳.以下是我使用的型号: class Test include Mongoid::Document field :time_stamp,type: BigDecimalend 以下是我用来创建文档的代码: aTime = "Wed Apr 24
我是Mongoid的新手.在我的模型文件中,我创建了一个数据类型为BigDecimal的字段.我想在其中存储时间戳.以下是我使用的型号:
class Test
  include Mongoid::Document
  field :time_stamp,type: BigDecimal
end

以下是我用来创建文档的代码:

aTime = "Wed Apr 24 09:48:38 +0000 2013"
timest = aTime.to_time.to_i
Test.create({time_stamp: timest})

我看到time_stamp在数据库中存储为String.任何人都可以指示我将时间戳记存储在数据库中,以便我可以对其进行一些操作.提前致谢.

解决方法

根据 this answer,MongoDB支持的数字类型有:
MongoDB stores data in a binary format called BSON which supports these numeric data types:

int32 - 4 bytes (32-bit signed integer)
int64 - 8 bytes (64-bit signed integer)
double - 8 bytes (64-bit IEEE 754 floating point)

在Mongoid documentation年加强此声明:

Types that are not supported as dynamic attributes since they cannot be cast are:

BigDecimal
Date
DateTime
Range

我不知道你想要的字段的东西,但如果你真的希望它存储为一个数字,你必须使用不同的数字类型,由MongoDB(BSON)支持,可能是浮点数或整数.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读