13

私はモンゴイドが初めてです。モデル ファイルで、データ型が 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 として格納されていることがわかります。タイムスタンプを数値としてDBに保存して、操作を実行できるように誰かに指示できますか。前もって感謝します。

4

1 に答える 1

2

この回答によると、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) でサポートされている別の数値型を使用する必要がありFloatますInteger

于 2013-04-26T21:54:47.063 に答える