Skip to content

Commit 2f2f9eb

Browse files
p-mongopEmily Giurleo
authored
MONGOID-4852 Add Mongoid 7.1 major changes/upgrade notes to the upgrade guide (#4744)
* MONGOID-4852 Add Mongoid 7.1 major changes/upgrade notes to the upgrade guide * suggested update to upgrade tutorial Co-authored-by: Oleg Pudeyev <oleg@bsdpower.com> Co-authored-by: Emily Giurleo <e.m.giurleo@gmail.com>
1 parent d6b504a commit 2f2f9eb

File tree

4 files changed

+240
-8
lines changed

4 files changed

+240
-8
lines changed

source/tutorials/mongoid-persistence.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,8 @@ are not invoked.
443443

444444
person.unset(:name)
445445

446+
.. _atomic-operation-grouping:
447+
446448
Atomic Operation Grouping
447449
*************************
448450

source/tutorials/mongoid-queries.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,8 @@ the default scope being evaluated first:
305305
embedded: false>
306306

307307

308+
.. _logical-operations:
309+
308310
Logical Operations
309311
******************
310312

@@ -562,6 +564,8 @@ is being added, if there already is a condition on the same field using the
562564
same operator, the operator expressions are combined according to the
563565
specified *merge strategy*.
564566

567+
.. _merge-strategies:
568+
565569
Merge Strategies
566570
````````````````
567571

source/tutorials/mongoid-upgrade.txt

Lines changed: 230 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,227 @@ Upgrading Mongoid
1010
:depth: 1
1111
:class: singlecol
1212

13-
Upgrading to 7.x Series
14-
```````````````````````
13+
14+
Upgrading to Mongoid 7.1
15+
------------------------
16+
17+
The following sections describe major changes in Mongoid 7.1.
18+
19+
Condition Combination
20+
`````````````````````
21+
22+
**Breaking change:** In Mongoid 7.1, when condition methods are invoked on a
23+
Criteria object, they always add new conditions to the existing conditions in
24+
the Criteria object. Previously new conditions could have replaced existing
25+
conditions in some circumstances.
26+
27+
Example Mongoid 7.1 behavior:
28+
29+
.. code-block:: ruby
30+
31+
Band.where(id: 1).where(id: 2)
32+
# => #<Mongoid::Criteria
33+
# selector: {"_id"=>1, "$and"=>[{"_id"=>2}]}
34+
# options: {}
35+
# class: Band
36+
# embedded: false>
37+
38+
Corresponding Mongoid 7.0 behavior:
39+
40+
.. code-block:: ruby
41+
42+
Band.where(id: 1).where(id: 2)
43+
# => #<Mongoid::Criteria
44+
# selector: {"_id"=>2}
45+
# options: {}
46+
# class: Band
47+
# embedded: false>
48+
49+
Logical Operations
50+
``````````````````
51+
52+
**Breaking change:** In Mongoid 7.1 logical operator methods ``or`` and
53+
``nor`` treat the receiver as one of the operands. This behavior matches that
54+
of ActiveRecord. Previously, ``or`` and ``nor`` added their parameters as
55+
additional conditions to the receiver.
56+
57+
Example Mongoid 7.1 behavior:
58+
59+
.. code-block:: ruby
60+
61+
Band.where(name: 'One').or(name: 'Two')
62+
# => #<Mongoid::Criteria
63+
# selector: {"$or"=>[{"name"=>"One"}, {"name"=>"Two"}]}
64+
# options: {}
65+
# class: Band
66+
# embedded: false>
67+
68+
Corresponding Mongoid 7.0 behavior:
69+
70+
.. code-block:: ruby
71+
72+
Band.where(name: 'One').or(name: 'Two')
73+
# => #<Mongoid::Criteria
74+
# selector: {"name"=>"One", "$or"=>[{"name"=>"Two"}]}
75+
# options: {}
76+
# class: Band
77+
# embedded: false>
78+
79+
To add ``or`` or ``nor`` parameters to the existing query without disjuncting
80+
the existing query, create a separate scope:
81+
82+
.. code-block:: ruby
83+
84+
Band.where(name: 'One').and(Band.or({name: 'Two'}, {name: 'Three'}))
85+
# => #<Mongoid::Criteria
86+
# selector: {"name"=>"One", "$and"=>[{"$or"=>[{"name"=>"Two"}, {"name"=>"Three"}]}]}
87+
# options: {}
88+
# class: Band
89+
# embedded: false>
90+
91+
Alternatively, use ``any_of`` which behaves as ``or`` did in Mongoid 7.0:
92+
93+
.. code-block:: ruby
94+
95+
Band.where(name: 'One').any_of({name: 'Two'}, {name: 'Three'})
96+
# => #<Mongoid::Criteria
97+
# selector: {"name"=>"One", "$or"=>[{"name"=>"Two"}, {"name"=>"Three"}]}
98+
# options: {}
99+
# class: Band
100+
# embedded: false>
101+
102+
For more information, please review the :ref:`logical operations
103+
<logical-operations>` section of the documentation.
104+
105+
Merge Strategies
106+
````````````````
107+
108+
**Breaking change:** In Mongoid 7.1, :ref:`Merge strategies <merge-strategies>`
109+
must be explicitly requested. Previously, ``all`` defaulted to the
110+
union strategy and ``in`` and ``nin`` defauled to the intersect strategy.
111+
In Mongoid 7.1, there is no merge strategy applied by default.
112+
113+
Example Mongoid 7.1 behavior:
114+
115+
.. code-block:: ruby
116+
117+
Band.all(a: 1).all(a: 2)
118+
# => #<Mongoid::Criteria
119+
# selector: {"a"=>{"$all"=>[1]}, "$and"=>[{"a"=>{"$all"=>[2]}}]}
120+
# options: {}
121+
# class: Band
122+
# embedded: false>
123+
124+
Default Mongoid 7.0 behavior:
125+
126+
.. code-block:: ruby
127+
128+
Band.all(a: 1).all(a: 2)
129+
# => #<Mongoid::Criteria
130+
# selector: {"a"=>{"$all"=>[1, 2]}}
131+
# options: {}
132+
# class: Band
133+
# embedded: false>
134+
135+
To achieve the same behavior in Mongoid 7.1, the desired merge strategy must be
136+
explicitly requested:
137+
138+
.. code-block:: ruby
139+
140+
Band.all(a: 1).union.all(a: 2)
141+
# => #<Mongoid::Criteria
142+
# selector: {"a"=>{"$all"=>[1, 2]}}
143+
# options: {}
144+
# class: Band
145+
# embedded: false>
146+
147+
Required Condition Arguments
148+
````````````````````````````
149+
150+
**Breaking change:** ``nil`` arguments to Criteria methods are no longer
151+
accepted. For example, the following invocation is now an error:
152+
153+
.. code-block:: ruby
154+
155+
Band.where(nil)
156+
157+
**Breaking change:** Most Criteria methods (other than logical operations)
158+
can no longer be called without arguments. For example, the following
159+
invocation is now an error:
160+
161+
.. code-block:: ruby
162+
163+
Band.in
164+
165+
``and``, ``or``, ``nor``, ``not``, ``any_of`` and ``where`` can be called
166+
without arguments.
167+
168+
Generated Queries
169+
`````````````````
170+
171+
Minor change: Mongoid 7.1 will simplify the Criteria selectors where possible
172+
by eliding unnecessary logical operators, typically ``$and``.
173+
174+
Example Mongoid 7.1 behavior:
175+
176+
.. code-block:: ruby
177+
178+
Band.where(year: 2000).and(name: 'One')
179+
# => #<Mongoid::Criteria
180+
# selector: {"year"=>2000, "name"=>"One"}
181+
# options: {}
182+
# class: Band
183+
# embedded: false>
184+
185+
Corresponding Mongoid 7.0 behavior:
186+
187+
.. code-block:: ruby
188+
189+
Band.where(year: 2000).and(name: 'One')
190+
# => #<Mongoid::Criteria
191+
# selector: {"year"=>2000, "$and"=>[{"name"=>"One"}]}
192+
# options: {}
193+
# class: Band
194+
# embedded: false>
195+
196+
Mongoid 7.1 also takes steps to produce the same selector shapes when
197+
semantically the same query is constructed using different code paths. For
198+
example, the following two approaches result in the same generated selector:
199+
200+
.. code-block:: ruby
201+
202+
Band.where(name: /ne/).and(name: 'One')
203+
204+
Band.and({name: /ne/}, {name: 'One'})
205+
206+
# => #<Mongoid::Criteria
207+
# selector: {"name"=>/ne/, "$and"=>[{"name"=>"One"}]}
208+
# options: {}
209+
# class: Band
210+
# embedded: false>
211+
212+
Atomic Operation Grouping
213+
`````````````````````````
214+
215+
Improvement: :ref:`Multiple atomic operations can now be grouped
216+
<atomic-operation-grouping>` to be executed as a single atomic operation.
217+
218+
Sharding
219+
````````
220+
221+
Improvement: :ref:`Shard key declarations <shard-keys>` now support hashed
222+
shard keys, and a Rake task to shard collection has been added to the
223+
:ref:`sharding management Rake tasks <sharding-management>`.
224+
225+
Ruby Version Support
226+
````````````````````
227+
228+
As of version 7.1, Mongoid supports Ruby 2.3+ and JRuby 9.2.
229+
Support for Ruby 2.2 and JRuby 9.1 has been dropped.
230+
231+
232+
Upgrading to Mongoid 7.0
233+
------------------------
15234

16235
This major version of Mongoid has a number of significant refactors, bug fixes
17236
and behavior corrections.
@@ -22,18 +241,21 @@ and version
22241
`7.0.0.beta <https://github.com/mongodb/mongoid/releases/tag/v7.0.0.beta>`_
23242
for specific changes that might be required in your code.
24243

25-
Upgrading to 6.x Series
26-
```````````````````````
27-
Mongoid 6.x is compatible with Rails 5 and requires Ruby 2.2.2 or higher.
244+
245+
Upgrading to Mongoid 6
246+
----------------------
247+
248+
Mongoid 6 is compatible with Rails 5 and requires Ruby 2.2.2 or higher.
28249

29250
Please see the list of behavior changes for version
30251
`6.0.0.rc0 <https://github.com/mongodb/mongoid/releases/tag/v6.0.0.rc0>`_
31252
and version
32253
`6.0.0.beta <https://github.com/mongodb/mongoid/releases/tag/v6.0.0.beta>`_
33254
for specific changes that might be required in your code.
34255

35-
Upgrading to 5.x Series
36-
```````````````````````
256+
257+
Upgrading to Mongoid 5
258+
----------------------
37259

38260
The underlying driver has changed from Moped to the official MongoDB Ruby driver.
39261
For all users dropping down to the driver level for operations, please see the driver
@@ -57,5 +279,5 @@ MongoDB 2.6 onward.
57279
provided. In order to guarantee that a document is the first or last, it needs to now
58280
contain an explicit sort.
59281

60-
Mongoid 5.x supports MongoDB 2.4 and higher only - MongoDB 2.2 is no longer
282+
Mongoid 5 supports MongoDB 2.4 and higher only - MongoDB 2.2 is no longer
61283
supported.

source/tutorials/sharding.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Sharding
1212
Mongoid can assist with setting up collection sharding in sharded environments.
1313

1414

15+
.. _shard-keys:
16+
1517
Declaring Shard Keys
1618
--------------------
1719

@@ -78,6 +80,8 @@ configured in the association as the field name:
7880
end
7981

8082

83+
.. _sharding-management:
84+
8185
Sharding Management Rake Tasks
8286
------------------------------
8387

0 commit comments

Comments
 (0)