-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathmongoid-9.0.txt
More file actions
330 lines (228 loc) · 10.5 KB
/
Copy pathmongoid-9.0.txt
File metadata and controls
330 lines (228 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
***********
Mongoid 9.0
***********
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 2
:class: singlecol
This page describes significant changes and improvements in Mongoid 9.0.
The complete list of releases is available `on GitHub
<https://github.com/mongodb/mongoid/releases>`_ and `in JIRA
<https://jira.mongodb.org/projects/MONGOID?selectedItem=com.atlassian.jira.jira-projects-plugin:release-page>`_;
please consult GitHub releases for detailed release notes and JIRA for
the complete list of issues fixed in each release, including bug fixes.
``touch`` method now clears changed state
-----------------------------------------
In Mongoid 8.x and older ``touch`` method leaves models in the changed state:
.. code-block:: ruby
# Mongoid 8.x behaviour
band = Band.create!
band.touch
band.changed? # => true
band.changes # => {"updated_at"=>[2023-01-30 13:12:57.477191135 UTC, 2023-01-30 13:13:11.482975646 UTC]}
Starting from 9.0 Mongoid now correctly clears changed state after using ``touch``
method.
.. code-block:: ruby
# Mongoid 9.0 behaviour
band = Band.create!
band.touch
band.changed? # => false
band.changes # => {}
Sandbox Mode for Rails Console
------------------------------
Mongoid now supports Rails console sandbox mode. If the Rails console started
with ``--sandbox`` flag, Mongoid starts a transaction on the ``:default`` client
before opening the console. This transaction won't be committed; therefore, all
the commands executed in the console using the ``:default`` client won't
be persisted in the database.
.. note::
If you execute commands in the sandbox mode *using any other client than default*,
these changes will be persisted as usual.
New Transactions API
--------------------
Mongoid 9.0 introduces new transactions API that is inspired by ActiveRecord:
.. code-block:: ruby
Band.transaction do
Band.create(title: 'Led Zeppelin')
end
band = Band.create(title: 'Deep Purple')
band.transaction do
band.active = false
band.save!
end
Please consult :ref:`transactions documentation <transactions>` for more details.
Embedded Documents Always Use Parent Persistence Context
--------------------------------------------------------
Mongoid 8.x and older allows user to specify persistence context for an
embedded document (using ``store_in`` macro). In Mongoid 9.0 these settings are
ignored for embedded documents; an embedded document now always uses the persistence
context of its parent.
Support for Passing Raw Values into Queries
-------------------------------------------
When performing queries, it is now possible skip Mongoid's type coercion logic
using the ``Mongoid::RawValue`` wrapper class. This can be useful when legacy
data in the database is of a different type than the field definition.
.. code-block:: ruby
class Person
include Mongoid::Document
field :age, type: Integer
end
# Query for the string "42", not the integer 42
Person.where(age: Mongoid::RawValue("42"))
Raise AttributeNotLoaded error when accessing fields omitted from query projection
----------------------------------------------------------------------------------
When attempting to access a field on a model instance which was
excluded with the ``.only`` or ``.without`` query projections methods
when the instance was loaded, Mongoid will now raise a
``Mongoid::Errors::AttributeNotLoaded`` error.
.. code-block:: ruby
Band.only(:name).first.label
#=> raises Mongoid::Errors::AttributeNotLoaded
Band.without(:label).first.label = 'Sub Pop Records'
#=> raises Mongoid::Errors::AttributeNotLoaded
In earlier Mongoid versions, the same conditions would raise an
``ActiveModel::MissingAttributeError``. Please check your code for
any Mongoid-specific usages of this class, and change them to
``Mongoid::Errors::AttributeNotLoaded``. Note additionally that
``AttributeNotLoaded`` inherits from ``Mongoid::Errors::MongoidError``,
while ``ActiveModel::MissingAttributeError`` does not.
Use configured time zone to typecast Date to Time in queries
-------------------------------------------------------------
When querying for a Time field using a Date value, Mongoid now correctly
considers the ``Mongoid.use_activesupport_time_zone`` configuration option
to perform type conversion.
.. code-block:: ruby
Mongoid.use_activesupport_time_zone = true
class Magazine
include Mongoid::Document
field :published_at, type: Time
end
Time.zone = 'Asia/Tokyo'
Magazine.gte(published_at: Date.parse('2022-09-26'))
#=> will return all results on or after Sept 26th, 2022
# at 0:00 in Asia/Tokyo time zone.
In prior Mongoid versions, the above code would ignore the
``Mongoid.use_activesupport_time_zone`` setting and behave as if
it were false, i.e. always using the system time zone to perform
the type conversion.
Note that in prior Mongoid versions, typecasting Date to Time during
persistence operations was already correctly using the
``Mongoid.use_activesupport_time_zone`` setting.
```#touch`` method on embedded documents correctly handles ``touch: false`` option
----------------------------------------------------------------------------------
When the ``touch: false`` option is set on an ``embedded_in`` relation,
calling the ``#touch`` method on an embedded child document will not
invoke ``#touch`` on its parent document.
.. code-block:: ruby
class Address
include Mongoid::Document
include Mongoid::Timestamps
embedded_in :mall, touch: false
end
class Mall
include Mongoid::Document
include Mongoid::Timestamps
embeds_many :addresses
end
mall = Mall.create!
address = mall.addresses.create!
address.touch
#=> updates address.updated_at but not mall.updated_at
In addition, the ``#touch`` method has been optimized to perform one
persistence operation per parent document, even when using multiple
levels of nested embedded documents.
``embedded_in`` associations now default to ``touch: true``
-----------------------------------------------------------
Updating an embedded subdocument will now automatically touch the parent,
unless you explicitly set ``touch: false`` on the relation:
.. code-block:: ruby
class Address
include Mongoid::Document
include Mongoid::Timestamps
embedded_in :mall, touch: false
end
For all other associations, the default remains ``touch: false``.
Flipped default for ``:replace`` option in ``#upsert``
------------------------------------------------------
Mongoid 8.1 added the ``:replace`` option to the ``#upsert`` method. This
option was used to specify whether or not the existing document should be
updated or replaced.
Mongoid 9.0 flips the default of this flag from ``true`` => ``false``.
This means that, by default, Mongoid 9 will update the existing document and
will not replace it.
The immutability of the ``_id`` field is now enforced
-----------------------------------------------------
Prior to Mongoid 9.0, mutating the ``_id`` field behaved inconsistently
depending on whether the document was top-level or embedded, and depending on
how the update was performed. As of 9.0, changing the ``_id`` field will now
raise an exception when the document is saved, if the document had been
previously persisted.
Mongoid 9.0 also introduces a new feature flag, ``immutable_ids``, which
defaults to ``true``.
.. code-block:: ruby
Mongoid::Config.immutable_ids = true
When set to false, the older, inconsistent behavior is restored.
Support for Defining Custom Field Type Values
---------------------------------------------
Mongoid 9.0 adds the ability to define custom ``field :type`` Symbol values as follows:
.. code-block:: ruby
# in /config/initializers/mongoid.rb
Mongoid.configure do |config|
config.field_type :point, Point
end
Refer to the :ref:`docs <http://docs.mongodb.org/manual/reference/fields/#custom-field-types>` for details.
Rename error InvalidFieldType to UnknownFieldType
-------------------------------------------------
The error class InvalidFieldType has been renamed to UnknownFieldType
to improve clarity. This error occurs when attempting using the
``field`` macro in a Document definition with a ``:type`` Symbol that
does not correspond to any built-in or custom-defined field type.
.. code-block:: ruby
class User
include Mongoid::Document
field :name, type: :bogus
#=> raises Mongoid::Errors::UnknownFieldType
end
Support for Defining Custom Field Options via Top-Level Config
--------------------------------------------------------------
Mongoid 9.0 adds the ability to define custom ``field`` options as follows:
.. code-block:: ruby
# in /config/initializers/mongoid.rb
Mongoid.configure do |config|
config.field_option :max_length do |model, field, value|
model.validates_length_of field.name, maximum: value
end
end
In Mongoid 8, this was possible with the following legacy syntax. Users are
recommended to migrate to the Mongoid 9.0 syntax above.
.. code-block:: ruby
Mongoid::Fields.option :max_length do |model, field, value|
model.validates_length_of field.name, maximum: value
end
Refer to the :ref:`docs <http://docs.mongodb.org/manual/reference/fields/#custom-field-options>` for details.
Bug Fixes and Improvements
--------------------------
This section will be for smaller bug fixes and improvements:
- The ``.unscoped`` method now also clears scopes declared using ``.with_scope``
`MONGOID-5214 <https://jira.mongodb.org/browse/MONGOID-5214>`_.
- When evolving a ``String`` to a ``BigDecimal`` (i.e. when querying a
``BigDecimal`` field with a ``String`` object), if the
``map_big_decimal_to_decimal128`` flag set to true, the conversion will
return a ``BSON::Decimal128`` and not a ``String``
`MONGOID-5484 <https://jira.mongodb.org/browse/MONGOID-5484>`_.
- Created new error ``Mongoid::Errors::InvalidEstimatedCountCriteria`` for
when calling ``estimated_document_count`` on a document class with a
default scope
`MONGOID-4960 <https://jira.mongodb.org/browse/MONGOID-4960>`_.
- Mongoid now uses primary reads for validations in all cases
`MONGOID-5150 <https://jira.mongodb.org/browse/MONGOID-5150>`_.
- Added support for symbol keys in localized field translation hashes
`MONGOID-5334 <https://jira.mongodb.org/browse/MONGOID-5334>`_.
- Added index wildcard option
`MONGOID-5388 <https://jira.mongodb.org/browse/MONGOID-5388>`_.
- With the ``map_big_decimal_to_decimal128`` flag set to false, ``demongoizing``
a non-numeric, non-string value that implements ``:to_d`` will return a string
rather than a ``BigDecimal``
`MONGOID-5507 <https://jira.mongodb.org/browse/MONGOID-5507>`_.