Skip to content

Commit d456304

Browse files
p-mongop
andauthored
Fix MONGOID-5054 $eq or $regex must be used in place of implicit equality based on value type (#4986)
Co-authored-by: Oleg Pudeyev <oleg@bsdpower.com>
1 parent ee0ca0c commit d456304

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

source/tutorials/mongoid-upgrade.txt

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,14 @@ Mongoid 7.2 behavior:
185185
# embedded: false>
186186

187187

188-
Condition Combination Using ``$eq``
189-
-----------------------------------
188+
Condition Combination Using ``$eq`` / ``$regex``
189+
------------------------------------------------
190190

191191
Minor change: when using the ``where``, ``and``, ``or``, and ``nor`` methods
192192
on ``Criteria`` objects and providing multiple conditions on the same field
193193
in the same argument using the symbol operator syntax, conditions may be
194-
combined using ``$eq`` instead of ``$and``.
194+
combined using ``$eq`` or ``$regex`` operators, as appropriate, instead of
195+
``$and``.
195196

196197
Mongoid 7.3 behavior:
197198

@@ -205,6 +206,14 @@ Mongoid 7.3 behavior:
205206
# class: Band
206207
# embedded: false>
207208

209+
Band.where(name: /A/, :name.ne => 'Astral')
210+
# =>
211+
# #<Mongoid::Criteria
212+
# selector: {"name"=>{"$regex"=>/A/, "$ne"=>"Astral"}}
213+
# options: {}
214+
# class: Band
215+
# embedded: false>
216+
208217
Mongoid 7.2 behavior:
209218

210219
.. code-block:: ruby
@@ -217,6 +226,17 @@ Mongoid 7.2 behavior:
217226
# class: Band
218227
# embedded: false>
219228

229+
Band.where(name: /A/, :name.ne => 'Astral')
230+
# =>
231+
# #<Mongoid::Criteria
232+
# selector: {"name"=>/A/, "$and"=>[{"name"=>{"$ne"=>"Astral"}}]}
233+
# options: {}
234+
# class: Band
235+
# embedded: false>
236+
237+
The ``$regex`` operator is used when the value is a regular expression, i.e.
238+
an instance of ``Regexp`` or ``BSON::Regexp::Raw`` classes.
239+
220240
When using the ``not`` method with multiple conditions provided in the same
221241
argument, the conditions are kept together and negated as a group.
222242

0 commit comments

Comments
 (0)