@@ -55,34 +55,40 @@ on a person by using the ``field`` macro.
55
55
56
56
class Person
57
57
include Mongoid::Document
58
- field :name, type: String
59
- field :date_of_birth, type: Date
60
- field :weight, type: Float
58
+ field :name, type: :string
59
+ field :date_of_birth, type: :date
60
+ field :weight, type: :float
61
61
end
62
62
63
63
Below is a list of valid types for fields.
64
64
65
- - ``Array``
66
- - ``BigDecimal``
67
- - ``Boolean``
68
- - ``Date``
69
- - ``DateTime``
70
- - ``Float``
71
- - ``Hash``
72
- - ``Integer``
73
- - ``BSON::ObjectId``
74
- - ``BSON::Binary``
75
- - ``Range``
76
- - ``Regexp``
77
- - ``Set``
78
- - ``String``
79
- - ``StringifiedSymbol``
80
- - ``Symbol``
81
- - ``Time``
82
- - ``TimeWithZone``
65
+ - ``:array``
66
+ - ``:big_decimal``
67
+ - ``:boolean``
68
+ - ``:date``
69
+ - ``:date_time``
70
+ - ``:decimal128`` (uses ``BSON::Decimal128``)
71
+ - ``:float``
72
+ - ``:hash``
73
+ - ``:integer``
74
+ - ``:object_id`` (uses ``BSON::ObjectID``)
75
+ - ``:binary`` (uses ``BSON::Binary``)
76
+ - ``:range``
77
+ - ``:regexp``
78
+ - ``:set``
79
+ - ``:string``
80
+ - ``:stringified_symbol`` (see below)
81
+ - ``:symbol``
82
+ - ``:time``
83
+ - ``:time_with_zone``
83
84
84
85
To define custom field types, refer to :ref:`Custom Field Types <custom-field-types>` below.
85
86
87
+ As of Mongoid 8.0, ``field :type`` should be specified as a ``Symbol``.
88
+ Specifying as a ``Class`` is deprecated and will be no longer supported in a
89
+ future major version of Mongoid. Unrecognized field type symbols will result
90
+ in an `InvalidFieldType` error when the model class is loaded.
91
+
86
92
87
93
.. _omitting-field-type-definition:
88
94
@@ -116,16 +122,16 @@ Types that are not supported as dynamic attributes since they cannot be cast are
116
122
117
123
.. _field-type-stringified-symbol:
118
124
119
- Field Type: StringifiedSymbol
120
- -----------------------------
125
+ Field Type :stringified_symbol
126
+ ------------------------------
121
127
122
- The ``StringifiedSymbol `` field type is the recommended field type for storing
123
- values that should be exposed as symbols to Ruby applications. When using the ``Symbol `` field type,
128
+ The ``:stringified_symbol `` field type is the recommended field type for storing
129
+ values that should be exposed as symbols to Ruby applications. When using the ``:symbol `` field type,
124
130
Mongoid defaults to storing values as BSON symbols. For more information on the
125
131
BSON symbol type, see :ref:`here <field-type-symbol>`.
126
132
However, the BSON symbol type is deprecated and is difficult to work with in programming languages
127
- without native symbol types, so the ``StringifiedSymbol `` type allows the use of symbols
128
- while ensuring interoperability with other drivers. The ``StringifiedSymbol `` type stores all data
133
+ without native symbol types, so the ``:stringified_symbol `` type allows the use of symbols
134
+ while ensuring interoperability with other drivers. The ``:stringified_symbol `` type stores all data
129
135
on the database as strings, while exposing values to the application as symbols.
130
136
131
137
An example usage is shown below:
@@ -170,15 +176,15 @@ migration from fields that currently store either strings or BSON symbols in the
170
176
171
177
.. _field-type-symbol:
172
178
173
- Field Type: Symbol
179
+ Field Type :symbol
174
180
------------------
175
181
176
- New applications should use the :ref:`StringifiedSymbol field type <field-type-stringified-symbol>`
177
- to store Ruby symbols in the database. The ``StringifiedSymbol `` field type
182
+ New applications should use the :ref:`:stringified_symbol field type <field-type-stringified-symbol>`
183
+ to store Ruby symbols in the database. The ``:stringified_symbol `` field type
178
184
provides maximum compatibility with other applications and programming languages
179
185
and has the same behavior in all circumstances.
180
186
181
- Mongoid also provides the deprecated ``Symbol `` field type for serializing
187
+ Mongoid also provides the deprecated ``:symbol `` field type for serializing
182
188
Ruby symbols to BSON symbols. Because the BSON specification deprecated the
183
189
BSON symbol type, the `bson` gem will serialize Ruby symbols into BSON strings
184
190
when used on its own. However, in order to maintain backwards compatibility
@@ -201,10 +207,10 @@ snippet in your project:
201
207
202
208
.. _field-type-hash:
203
209
204
- Field Type: Hash
210
+ Field Type :hash
205
211
----------------
206
212
207
- When using a field of type Hash , be wary of adhering to the
213
+ When using a field of type ``:hash`` , be wary of adhering to the
208
214
`legal key names for mongoDB <http://docs.mongodb.org/manual/reference/limits/#naming-restrictions>`_,
209
215
or else the values will not store properly.
210
216
@@ -213,7 +219,7 @@ or else the values will not store properly.
213
219
class Person
214
220
include Mongoid::Document
215
221
field :first_name
216
- field :url, type: Hash
222
+ field :url, type: :hash
217
223
218
224
# will update the fields properly and save the values
219
225
def set_vals
@@ -233,21 +239,21 @@ or else the values will not store properly.
233
239
234
240
.. _field-type-time:
235
241
236
- Field Type: Time
242
+ Field Type :time
237
243
----------------
238
244
239
- ``Time `` fields store values as ``Time`` instances in the :ref:`configured
245
+ ``:time `` fields store values as ``Time`` instances in the :ref:`configured
240
246
time zone <time-zones>`.
241
247
242
248
``Date`` and ``DateTime`` instances are converted to ``Time`` instances upon
243
- assignment to a ``Time `` field:
249
+ assignment to a ``:time `` field:
244
250
245
251
.. code-block:: ruby
246
252
247
253
class Voter
248
254
include Mongoid::Document
249
-
250
- field :registered_at, type: Time
255
+
256
+ field :registered_at, type: :time
251
257
end
252
258
253
259
Voter.new(registered_at: Date.today)
@@ -259,10 +265,10 @@ local time, because the application was not configured to use UTC times.
259
265
260
266
.. _field-type-date:
261
267
262
- Field Type: Date
268
+ Field Type :date
263
269
----------------
264
270
265
- Mongoid allows assignment of values of several types to ``Date `` fields:
271
+ Mongoid allows assignment of values of several types to ``:date `` fields:
266
272
267
273
- ``Date`` - the provided date is stored as is.
268
274
- ``Time``, ``DateTime``, ``ActiveSupport::TimeWithZone`` - the date component
@@ -280,20 +286,20 @@ As a date & time to date conversion is lossy (it discards the time component),
280
286
especially if an application operates with times in different time zones it is
281
287
recommended to explicitly convert ``String``, ``Time`` and ``DateTime``
282
288
objects to ``Date`` objects before assigning the values to fields of type
283
- ``Date ``.
289
+ ``:date ``.
284
290
285
291
286
292
.. _field-type-date-time:
287
293
288
- Field Type: DateTime
294
+ Field Type :date_time
289
295
---------------------
290
296
291
297
MongoDB stores all times as UTC timestamps. When assigning a value to a
292
- ``DateTime `` field, or when querying a ``DateTime `` field, Mongoid
298
+ ``:date_time `` field, or when querying a ``:date_time `` field, Mongoid
293
299
converts the passed in value to a UTC ``Time`` before sending it to the
294
300
MongoDB server.
295
301
296
- ``Time``, ``ActiveSupport::TimeWithZone`` and ``DateTime`` objects embed
302
+ ``Time``, ``ActiveSupport::TimeWithZone``, and ``DateTime`` objects embed
297
303
time zone information, and the value persisted is the specified moment in
298
304
time, in UTC. When the value is retrieved, the time zone in which it is
299
305
returned is defined by the :ref:`configured time zone settings <time-zones>`.
@@ -302,7 +308,7 @@ returned is defined by the :ref:`configured time zone settings <time-zones>`.
302
308
303
309
class Ticket
304
310
include Mongoid::Document
305
- field :opened_at, type: DateTime
311
+ field :opened_at, type: :date_time
306
312
end
307
313
308
314
Mongoid.use_activesupport_time_zone = true
@@ -332,7 +338,7 @@ doing so, the integers/floats are assumed to be Unix timestamps (in UTC):
332
338
ticket.opened_at
333
339
# => Fri, 14 Dec 2018 16:12:54 +0000
334
340
335
- If a string is used as a ``DateTime `` field value, the behavior depends on
341
+ If a ``String`` is used as a ``:date_time `` field value, the behavior depends on
336
342
whether the string includes a time zone. If no time zone is specified,
337
343
the :ref:`default Mongoid time zone <time-zones>` is used:
338
344
@@ -354,7 +360,7 @@ If a time zone is specified, it is respected:
354
360
355
361
.. _field-type-regexp:
356
362
357
- Field Type: Regexp
363
+ Field Type :regexp
358
364
------------------
359
365
360
366
MongoDB supports storing regular expressions in documents, and querying using
@@ -365,7 +371,7 @@ fork of `Oniguruma regular expression engine <https://github.com/kkos/oniguruma>
365
371
The two regular expression implementations generally provide equivalent
366
372
functionality but have several important syntax differences.
367
373
368
- When a field is declared to be of type Regexp , Mongoid converts Ruby regular
374
+ When a field is declared to be of type ``:regexp`` , Mongoid converts Ruby regular
369
375
expressions to BSON regular expressions and stores the result in MongoDB.
370
376
Retrieving the field from the database produces a ``BSON::Regexp::Raw``
371
377
instance:
@@ -375,7 +381,7 @@ instance:
375
381
class Token
376
382
include Mongoid::Document
377
383
378
- field :pattern, type: Regexp
384
+ field :pattern, type: :regexp
379
385
end
380
386
381
387
token = Token.create!(pattern: /hello.world/m)
@@ -847,9 +853,20 @@ can use in our model class as follows:
847
853
848
854
class Profile
849
855
include Mongoid::Document
850
- field :location, type: Point
856
+ field :location, type: :point
857
+ end
858
+
859
+ First, declare the new field type mapping in an initializer:
860
+
861
+ .. code-block:: ruby
862
+
863
+ # in /config/initializers/mongoid_custom_fields.rb
864
+
865
+ Mongoid::Fields.configure do
866
+ type :point, Point
851
867
end
852
868
869
+
853
870
Then make a Ruby class to represent the type. This class must define methods
854
871
used for MongoDB serialization and deserialization as follows:
855
872
@@ -945,8 +962,10 @@ specifiying its handler function as a block:
945
962
946
963
# in /config/initializers/mongoid_custom_fields.rb
947
964
948
- Mongoid::Fields.option :required do |model, field, value|
949
- model.validates_presence_of field if value
965
+ Mongoid::Fields.configure do
966
+ option :required do |model, field, value|
967
+ model.validates_presence_of field.name if value
968
+ end
950
969
end
951
970
952
971
Then, use it your model class:
0 commit comments