gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 09:27:20 26 4
gpt4 key购买 nike

我是 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com