senthil
senthil

Reputation: 1307

Storing Time Stamp as Number Mongoid

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.

Upvotes: 13

Views: 1062

Answers (1)

michelpm
michelpm

Reputation: 1845

According to this answer, the numeric types supported by MongoDB are:

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)

Reinforced by this statement in the Mongoid documentation:

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

BigDecimal
Date
DateTime
Range

I don't know about the things you want to with the field, but if you really want it stored as a number, you have to use a different numeric type that is supported by the MongoDB (BSON), probably Float or Integer.

Upvotes: 2

Related Questions