@@ -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 7.5, ``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
@@ -117,16 +123,16 @@ Types that are not supported as dynamic attributes since they cannot be cast are
117
123
118
124
.. _field-type-stringified-symbol:
119
125
120
- Field Type: StringifiedSymbol
121
- -----------------------------
126
+ Field Type :stringified_symbol
127
+ ------------------------------
122
128
123
- The ``StringifiedSymbol `` field type is the recommended field type for storing
124
- values that should be exposed as symbols to Ruby applications. When using the ``Symbol `` field type,
129
+ The ``:stringified_symbol `` field type is the recommended field type for storing
130
+ values that should be exposed as symbols to Ruby applications. When using the ``:symbol `` field type,
125
131
Mongoid defaults to storing values as BSON symbols. For more information on the
126
132
BSON symbol type, see :ref:`here <field-type-symbol>`.
127
133
However, the BSON symbol type is deprecated and is difficult to work with in programming languages
128
- without native symbol types, so the ``StringifiedSymbol `` type allows the use of symbols
129
- while ensuring interoperability with other drivers. The ``StringifiedSymbol `` type stores all data
134
+ without native symbol types, so the ``:stringified_symbol `` type allows the use of symbols
135
+ while ensuring interoperability with other drivers. The ``:stringified_symbol `` type stores all data
130
136
on the database as strings, while exposing values to the application as symbols.
131
137
132
138
An example usage is shown below:
@@ -171,15 +177,15 @@ migration from fields that currently store either strings or BSON symbols in the
171
177
172
178
.. _field-type-symbol:
173
179
174
- Field Type: Symbol
180
+ Field Type :symbol
175
181
------------------
176
182
177
- New applications should use the :ref:`StringifiedSymbol field type <field-type-stringified-symbol>`
178
- to store Ruby symbols in the database. The ``StringifiedSymbol `` field type
183
+ New applications should use the :ref:`:stringified_symbol field type <field-type-stringified-symbol>`
184
+ to store Ruby symbols in the database. The ``:stringified_symbol `` field type
179
185
provides maximum compatibility with other applications and programming languages
180
186
and has the same behavior in all circumstances.
181
187
182
- Mongoid also provides the deprecated ``Symbol `` field type for serializing
188
+ Mongoid also provides the deprecated ``:symbol `` field type for serializing
183
189
Ruby symbols to BSON symbols. Because the BSON specification deprecated the
184
190
BSON symbol type, the `bson` gem will serialize Ruby symbols into BSON strings
185
191
when used on its own. However, in order to maintain backwards compatibility
@@ -202,10 +208,10 @@ snippet in your project:
202
208
203
209
.. _field-type-hash:
204
210
205
- Field Type: Hash
211
+ Field Type :hash
206
212
----------------
207
213
208
- When using a field of type Hash , be wary of adhering to the
214
+ When using a field of type ``:hash`` , be wary of adhering to the
209
215
`legal key names for mongoDB <http://docs.mongodb.org/manual/reference/limits/#naming-restrictions>`_,
210
216
or else the values will not store properly.
211
217
@@ -214,7 +220,7 @@ or else the values will not store properly.
214
220
class Person
215
221
include Mongoid::Document
216
222
field :first_name
217
- field :url, type: Hash
223
+ field :url, type: :hash
218
224
219
225
# will update the fields properly and save the values
220
226
def set_vals
@@ -234,21 +240,21 @@ or else the values will not store properly.
234
240
235
241
.. _field-type-time:
236
242
237
- Field Type: Time
243
+ Field Type :time
238
244
----------------
239
245
240
- ``Time `` fields store values as ``Time`` instances in the :ref:`configured
246
+ ``:time `` fields store values as ``Time`` instances in the :ref:`configured
241
247
time zone <time-zones>`.
242
248
243
249
``Date`` and ``DateTime`` instances are converted to ``Time`` instances upon
244
- assignment to a ``Time `` field:
250
+ assignment to a ``:time `` field:
245
251
246
252
.. code-block:: ruby
247
253
248
254
class Voter
249
255
include Mongoid::Document
250
256
251
- field :registered_at, type: Time
257
+ field :registered_at, type: :time
252
258
end
253
259
254
260
Voter.new(registered_at: Date.today)
@@ -260,10 +266,10 @@ local time, because the application was not configured to use UTC times.
260
266
261
267
.. _field-type-date:
262
268
263
- Field Type: Date
269
+ Field Type :date
264
270
----------------
265
271
266
- Mongoid allows assignment of values of several types to ``Date `` fields:
272
+ Mongoid allows assignment of values of several types to ``:date `` fields:
267
273
268
274
- ``Date`` - the provided date is stored as is.
269
275
- ``Time``, ``DateTime``, ``ActiveSupport::TimeWithZone`` - the date component
@@ -281,20 +287,20 @@ As a date & time to date conversion is lossy (it discards the time component),
281
287
especially if an application operates with times in different time zones it is
282
288
recommended to explicitly convert ``String``, ``Time`` and ``DateTime``
283
289
objects to ``Date`` objects before assigning the values to fields of type
284
- ``Date ``.
290
+ ``:date ``.
285
291
286
292
287
293
.. _field-type-date-time:
288
294
289
- Field Type: DateTime
295
+ Field Type :date_time
290
296
---------------------
291
297
292
298
MongoDB stores all times as UTC timestamps. When assigning a value to a
293
- ``DateTime `` field, or when querying a ``DateTime `` field, Mongoid
299
+ ``:date_time `` field, or when querying a ``:date_time `` field, Mongoid
294
300
converts the passed in value to a UTC ``Time`` before sending it to the
295
301
MongoDB server.
296
302
297
- ``Time``, ``ActiveSupport::TimeWithZone`` and ``DateTime`` objects embed
303
+ ``Time``, ``ActiveSupport::TimeWithZone``, and ``DateTime`` objects embed
298
304
time zone information, and the value persisted is the specified moment in
299
305
time, in UTC. When the value is retrieved, the time zone in which it is
300
306
returned is defined by the :ref:`configured time zone settings <time-zones>`.
@@ -303,7 +309,7 @@ returned is defined by the :ref:`configured time zone settings <time-zones>`.
303
309
304
310
class Ticket
305
311
include Mongoid::Document
306
- field :opened_at, type: DateTime
312
+ field :opened_at, type: :date_time
307
313
end
308
314
309
315
Mongoid.use_activesupport_time_zone = true
@@ -333,7 +339,7 @@ doing so, the integers/floats are assumed to be Unix timestamps (in UTC):
333
339
ticket.opened_at
334
340
# => Fri, 14 Dec 2018 16:12:54 +0000
335
341
336
- If a string is used as a ``DateTime `` field value, the behavior depends on
342
+ If a ``String`` is used as a ``:date_time `` field value, the behavior depends on
337
343
whether the string includes a time zone. If no time zone is specified,
338
344
the :ref:`default Mongoid time zone <time-zones>` is used:
339
345
@@ -355,7 +361,7 @@ If a time zone is specified, it is respected:
355
361
356
362
.. _field-type-regexp:
357
363
358
- Field Type: Regexp
364
+ Field Type :regexp
359
365
------------------
360
366
361
367
MongoDB supports storing regular expressions in documents, and querying using
@@ -366,7 +372,7 @@ fork of `Oniguruma regular expression engine <https://github.com/kkos/oniguruma>
366
372
The two regular expression implementations generally provide equivalent
367
373
functionality but have several important syntax differences.
368
374
369
- When a field is declared to be of type Regexp , Mongoid converts Ruby regular
375
+ When a field is declared to be of type ``:regexp`` , Mongoid converts Ruby regular
370
376
expressions to BSON regular expressions and stores the result in MongoDB.
371
377
Retrieving the field from the database produces a ``BSON::Regexp::Raw``
372
378
instance:
@@ -376,7 +382,7 @@ instance:
376
382
class Token
377
383
include Mongoid::Document
378
384
379
- field :pattern, type: Regexp
385
+ field :pattern, type: :regexp
380
386
end
381
387
382
388
token = Token.create!(pattern: /hello.world/m)
@@ -747,9 +753,20 @@ can use in our model class as follows:
747
753
748
754
class Profile
749
755
include Mongoid::Document
750
- field :location, type: Point
756
+ field :location, type: :point
757
+ end
758
+
759
+ First, declare the new field type mapping in an initializer:
760
+
761
+ .. code-block:: ruby
762
+
763
+ # in /config/initializers/mongoid_custom_fields.rb
764
+
765
+ Mongoid::Fields.configure do
766
+ type :point, Point
751
767
end
752
768
769
+
753
770
Then make a Ruby class to represent the type. This class must define methods
754
771
used for MongoDB serialization and deserialization as follows:
755
772
@@ -845,8 +862,10 @@ specifiying its handler function as a block:
845
862
846
863
# in /config/initializers/mongoid_custom_fields.rb
847
864
848
- Mongoid::Fields.option :required do |model, field, value|
849
- model.validates_presence_of field if value
865
+ Mongoid::Fields.configure do
866
+ option :required do |model, field, value|
867
+ model.validates_presence_of field if value
868
+ end
850
869
end
851
870
852
871
Then, use it your model class:
0 commit comments