@@ -28,17 +28,14 @@ Field type definitions determine how Mongoid behaves when constructing queries
28
28
and retrieving/writing fields from/to the database. Specifically:
29
29
30
30
1. When assigning values to fields at runtime, the values are converted to the
31
- specified type.
32
-
31
+ specified type.
33
32
2. When persisting data to MongoDB, the data is sent in an appropriate
34
- type, permitting richer data manipulation within MongoDB or by other
35
- tools.
36
-
33
+ type, permitting richer data manipulation within MongoDB or by other
34
+ tools.
37
35
3. When querying documents, query parameters are converted to the specified
38
- type before being sent to MongoDB.
39
-
36
+ type before being sent to MongoDB.
40
37
4. When retrieving documents from the database, field values are converted
41
- to the specified type.
38
+ to the specified type.
42
39
43
40
Changing the field definitions in a model class does not alter data already stored in
44
41
MongoDB. To update type or contents of fields of existing documents,
@@ -63,25 +60,26 @@ on a person by using the ``field`` macro.
63
60
The valid types for fields are as follows:
64
61
65
62
- ``Array``
66
- - ``BigDecimal``
63
+ - ``BSON::Binary``
64
+ - :ref:`BigDecimal <field-type-big-decimal>`
67
65
- ``Mongoid::Boolean``, which may be specified simply as ``Boolean`` in the
68
66
scope of a class which included ``Mongoid::Document``.
69
- - `` Date` `
70
- - `` DateTime` `
67
+ - :ref:` Date <field-type-date> `
68
+ - :ref:` DateTime <field-type-date-time> `
71
69
- ``Float``
72
- - `` Hash` `
70
+ - :ref:` Hash <field-type-hash> `
73
71
- ``Integer``
72
+ - :ref:`Object <untyped-fields>`
74
73
- ``BSON::ObjectId``
75
- - ``BSON::Binary``
76
74
- ``Range``
77
- - `` Regexp` `
75
+ - :ref:` Regexp <field-type-regexp> `
78
76
- ``Set``
79
77
- ``String``
80
- - `` Mongoid::StringifiedSymbol``, which may be specified simply as
81
- ``StringifiedSymbol`` in the scope of a class which included
82
- ``Mongoid::Document``.
83
- - `` Symbol` `
84
- - `` Time` `
78
+ - :ref:` Mongoid::StringifiedSymbol <field-type-stringified-symbol>`,
79
+ which may be specified simply as ``StringifiedSymbol`` in the scope of a
80
+ class which included ``Mongoid::Document``.
81
+ - :ref:` Symbol <field-type-stringified-symbol> `
82
+ - :ref:` Time <field-type-time> `
85
83
- ``ActiveSupport::TimeWithZone``
86
84
87
85
Mongoid also recognizes the string ``"Boolean"`` as an alias for the
@@ -97,34 +95,60 @@ To define custom field types, refer to :ref:`Custom Field Types <custom-field-ty
97
95
``BSON::Decimal128`` will return values of type ``BSON::Decimal128`` in
98
96
BSON <=4 and values of type ``BigDecimal`` in BSON 5+.
99
97
100
- .. _omitting-field-type-definition:
101
98
102
- Omitting Field Type Definition
103
- ------------------------------
99
+ .. _untyped-fields:
104
100
105
- If you decide not to specify the type of field with the definition, Mongoid will treat
106
- it as an object and not try to typecast it when sending the values to the database.
107
- This can be advantageous as the lack of attempted conversion will yield a slight
108
- performance gain. However some types are not supported if not defined as fields.
109
- You can safely omit type specifications when:
101
+ Untyped Fields
102
+ --------------
110
103
111
- - You're not using a web front end and values are already properly cast.
112
- - All of your fields are strings.
104
+ Not specifying a type for a field is the same as specifying the ``Object``
105
+ type. Such fields are untyped:
113
106
114
107
.. code-block:: ruby
115
108
116
- class Person
117
- include Mongoid::Document
118
- field :first_name
119
- field :middle_name
120
- field :last_name
121
- end
109
+ class Product
110
+ include Mongoid::Document
111
+
112
+ field :properties
113
+ # Equivalent to:
114
+ field :properties, type: Object
115
+ end
122
116
123
- Types that are not supported as dynamic attributes since they cannot be cast are:
117
+ An untyped field can store values of any type which is directly serializable
118
+ to BSON. This is useful when a field may contain values of different types
119
+ (i.e. it is a variant type field), or when the type of values is not known
120
+ ahead of time:
124
121
125
- - ``Date``
126
- - ``DateTime``
127
- - ``Range``
122
+ .. code-block:: ruby
123
+
124
+ product = Product.new(properties: "color=white,size=large")
125
+ product.properties
126
+ # => "color=white,size=large"
127
+
128
+ product = Product.new(properties: {color: "white", size: "large"})
129
+ product.properties
130
+ # => {:color=>"white", :size=>"large"}
131
+
132
+ When values are assigned to the field, Mongoid still performs mongoization but
133
+ uses the class of the value rather than the field type for mongoization logic.
134
+
135
+ .. code-block:: ruby
136
+
137
+ product = Product.new(properties: 0..10)
138
+ product.properties
139
+ # The range 0..10, mongoized:
140
+ # => {"min"=>0, "max"=>10}
141
+
142
+ When reading data from the database, Mongoid does not perform any type
143
+ conversions on untyped fields. For this reason, even though it is possible
144
+ to write any BSON-serializable value into an untyped fields, values which
145
+ require special handling on the database reading side will generally not work
146
+ correctly in an untyped field. Among field types supported by Mongoid,
147
+ values of the following types should not be stored in untyped fields:
148
+
149
+ - ``Date`` (values will be returned as ``Time``)
150
+ - ``DateTime`` (values will be returned as ``Time``)
151
+ - ``Range`` (values will be returned as ``Hash``)
128
152
129
153
130
154
.. _field-type-stringified-symbol:
@@ -447,7 +471,7 @@ matches strings containing "hello" before a newline, besides strings ending in
447
471
This is because the meaning of ``$`` is different between PCRE and Ruby
448
472
regular expressions.
449
473
450
- .. _bigdecimal-fields :
474
+ .. _field-type-big-decimal :
451
475
452
476
BigDecimal Fields
453
477
-----------------
0 commit comments