13

I'm new to Mongoid. In my model file, I've created a field with data type BigDecimal. I want to store time stamp in it. Below is the model that I'm using:

class Test
  include Mongoid::Document
  field :time_stamp, type: BigDecimal
end

And Below is the code that I'm using to create a document:

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

I see that the time_stamp is stored as String in the database. Can anybody direct me to store the timestamp as number in DB so that I could perform some operations on it. Thanks in advance.

4

1 回答 1

2

根据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 文档中的此声明加强:

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

BigDecimal
Date
DateTime
Range

我不知道您想对该字段进行哪些操作,但如果您真的希望将其存储为数字,则必须使用 MongoDB (BSON) 支持的不同数字类型,可能FloatInteger.

于 2013-04-26T21:54:47.063 回答