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

标签 ruby-on-rails mongoid

我是 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})

我看到时间戳在数据库中存储为字符串。任何人都可以指示我将时间戳作为数字存储在数据库中,以便我可以对其执行一些操作。提前致谢。

最佳答案

根据 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) 支持的不同数字类型,可能是 FloatInteger .

关于ruby-on-rails - 将时间戳存储为 Number Mongoid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16189857/

相关文章:

ruby - Sinatra Mongoid 字符串不是有效的 UTF-8

ruby-on-rails - MongoDB/Mongoid : search for documents matching first item in array

ruby-on-rails - 单个规范在单独运行时通过,但在所有规范运行时失败

ruby-on-rails - 测试与 rspec : Category does not have a post_id foreign key 的关联

ruby-on-rails - 如何在 slim (rails) 中生成新的 View 文件?

ruby-on-rails - 轮胎似乎忽略了页面变量

ruby-on-rails - Rails中带有.SCSS的背景图像?

sql - 找不到 id=1 的项目 [WHERE "projects"."deleted_at"IS NULL]

ruby-on-rails - mongodb 无法创建/保存双字节语言

mongoid - 如何在 Mongoid 中重命名 mongo 集合?