@@ -147,6 +147,7 @@ Below is a list of valid types for fields.
147
147
- ``Regexp``
148
148
- ``Set``
149
149
- ``String``
150
+ - ``StringifiedSymbol``
150
151
- ``Symbol``
151
152
- ``Time``
152
153
- ``TimeWithZone``
@@ -176,6 +177,58 @@ Types that are not supported as dynamic attributes since they cannot be cast are
176
177
- ``DateTime``
177
178
- ``Range``
178
179
180
+ .. _stringified-symbol:
181
+
182
+ The StringifiedSymbol Type
183
+ --------------------------
184
+
185
+ The ``StringifiedSymbol`` field type is the recommended field type for storing
186
+ values that should be exposed as symbols to Ruby applications. When using the ``Symbol`` field type,
187
+ Mongoid defaults to storing values as BSON symbols. For more information on the
188
+ BSON symbol type, see :ref:`here <bson-symbol>`.
189
+ However, the BSON symbol type is deprecated and is difficult to work with in programming languages
190
+ without native symbol types, so the ``StringifiedSymbol`` type allows the use of symbols
191
+ while ensuring interoperability with other drivers. The ``StringifiedSymbol`` type stores all data
192
+ on the database as strings, while exposing values to the application as symbols.
193
+
194
+ An example usage is shown below:
195
+
196
+ .. code-block:: ruby
197
+
198
+ class Post
199
+ include Mongoid::Document
200
+
201
+ field :status, type: StringifiedSymbol
202
+ end
203
+
204
+ post = Post.new(status: :hello)
205
+ # status is stored as "hello" on the database, but returned as a Symbol
206
+ post.status
207
+ # => :hello
208
+
209
+ # String values can be assigned also:
210
+ post = Post.new(status: "hello")
211
+ # status is stored as "hello" on the database, but returned as a Symbol
212
+ post.status
213
+ # => :hello
214
+
215
+ All non-string values will be stringified upon being sent to the database (via ``to_s``), and
216
+ all values will be converted to symbols when returned to the application. Values that cannot be
217
+ converted directly to symbols, such as integers and arrays, will first be converted to strings and
218
+ then symbols before being returned to the application.
219
+
220
+ For example, setting an integer as ``status``:
221
+
222
+ .. code-block:: ruby
223
+
224
+ post = Post.new(status: 42)
225
+ post.status
226
+ # => :"42"
227
+
228
+ If the ``StringifiedSymbol`` type is applied to a field that contains BSON symbols, the values
229
+ will be stored as strings instead of BSON symbols on the next save. This permits transparent lazy
230
+ migration from fields that currently store either strings or BSON symbols in the database to the
231
+ ``StringifiedSymbol`` field type.
179
232
180
233
Updating Container Fields
181
234
-------------------------
@@ -706,35 +759,27 @@ For cases when you do not want to have ``BSON::ObjectId`` ids, you can override
706
759
field :_id, type: String, default: ->{ name }
707
760
end
708
761
762
+ .. _bson-symbol:
763
+
709
764
BSON Symbol Type
710
765
----------------
711
766
712
- Because the BSON specification deprecated the BSON symbol type, the `bson` gem will serialize Ruby
713
- symbols into BSON strings when used on its own. However, in order to maintain backwards
714
- compatibility with older datasets, the `mongo` gem overrides this behavior to serialize Ruby symbols as
715
- BSON symbols. This is necessary to be able to specify queries for documents which contain BSON
716
- symbols as fields.
717
-
718
- Although Mongoid allows applications to define fields with the Symbol type, this could present
719
- problems when using other MongoDB tools that have removed support for the type. Because of this,
720
- new applications should not specify model fields with the Symbol type but instead use the String
721
- type. Existing applications with Symbol model fields may convert those fields to Strings using one
722
- of the following methods:
723
-
724
- - Eager migration: write a script or rake task that queries each document and
725
- updates any symbols it finds to strings. This will take a long time for larger data sets, and
726
- unless the application can handle two different schemas existing in the data at once, it will
727
- need to be taken offline while the script runs.
728
-
729
- - Lazy migration: rather than updating all of the documents at once, each document received from a
730
- query should be reinserted if it contains a symbol; since the BSON received will have already
731
- turned the symbols into strings, this will update the document as necessary. This can be done
732
- by simply calling the ``save`` method. Note that this method will only convert documents that
733
- are accessed by the application, eventually eager migration may be needed to convert the
734
- remaining documents that haven't been accessed.
735
-
736
- To override default behaivor and configure the ``mongo`` gem (and thereby Mongoid as well)
737
- to encode symbol values as strings, include the following code snippet in your project:
767
+ New applications should use the :ref:`StringifiedSymbol field type <stringified-symbol>`
768
+ to store Ruby symbols in the database. The ``StringifiedSymbol`` field type
769
+ provides maximum compatibility with other applications and programming languages
770
+ and has the same behavior in all circumstances.
771
+
772
+ Mongoid also provides the deprecated ``Symbol`` field type for serializing
773
+ Ruby symbols to BSON symbols. Because the BSON specification deprecated the
774
+ BSON symbol type, the `bson` gem will serialize Ruby symbols into BSON strings
775
+ when used on its own. However, in order to maintain backwards compatibility
776
+ with older datasets, the `mongo` gem overrides this behavior to serialize Ruby
777
+ symbols as BSON symbols. This is necessary to be able to specify queries for
778
+ documents which contain BSON symbols as fields.
779
+
780
+ To override the default behavior and configure the ``mongo`` gem (and thereby
781
+ Mongoid as well) to encode symbol values as strings, include the following code
782
+ snippet in your project:
738
783
739
784
.. code-block:: ruby
740
785
0 commit comments