@@ -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
16235This major version of Mongoid has a number of significant refactors, bug fixes
17236and behavior corrections.
@@ -22,18 +241,21 @@ and version
22241`7.0.0.beta <https://github.com/mongodb/mongoid/releases/tag/v7.0.0.beta>`_
23242for 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
29250Please see the list of behavior changes for version
30251`6.0.0.rc0 <https://github.com/mongodb/mongoid/releases/tag/v6.0.0.rc0>`_
31252and version
32253`6.0.0.beta <https://github.com/mongodb/mongoid/releases/tag/v6.0.0.beta>`_
33254for 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
38260The underlying driver has changed from Moped to the official MongoDB Ruby driver.
39261For all users dropping down to the driver level for operations, please see the driver
@@ -57,5 +279,5 @@ MongoDB 2.6 onward.
57279provided. In order to guarantee that a document is the first or last, it needs to now
58280contain 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
61283supported.
0 commit comments