Skip to content

Commit 9c7132c

Browse files
p-mongop
andauthored
Fix MONGOID-5020 Condition lifting breaks with symbol operators on Ruby <= 2.6 (#4915)
* MONGOID-5020 test cases for operators with symbols * MONGOID-5020 convert hashes to indifferent access prior to query manipulation * MONGOID-5020 queries are now more uniform * MONGOID-5020 combine with $eq when conditions are given in the same argument * MONGOID-5020 test all matrix of string/symbol operator types * fix straggler test * document the situation * document $eq lifting Co-authored-by: Oleg Pudeyev <[email protected]>
1 parent 4c231b4 commit 9c7132c

File tree

1 file changed

+154
-2
lines changed

1 file changed

+154
-2
lines changed

source/tutorials/mongoid-upgrade.txt

Lines changed: 154 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,123 @@ please consult GitHub releases for detailed release notes and JIRA for
1818
the complete list of issues fixed in each release, including bug fixes.
1919

2020

21+
Upgrading to Mongoid 7.3
22+
========================
23+
24+
The following sections describe significant changes in Mongoid 7.3.
25+
26+
Field Operator Stringification
27+
------------------------------
28+
29+
Minor change: the ``and`` logical operator now stringifies field operators
30+
in its arguments. Mongoid 7.3 behavior:
31+
32+
.. code-block:: ruby
33+
34+
Band.and(year: {'$in': [2020]})
35+
# =>
36+
# #<Mongoid::Criteria
37+
# selector: {"year"=>{"$in"=>[2020]}}
38+
# options: {}
39+
# class: Band
40+
# embedded: false>
41+
42+
Mongoid 7.2 behavior:
43+
44+
.. code-block:: ruby
45+
46+
Band.and(year: {'$in': [2020]})
47+
# =>
48+
# #<Mongoid::Criteria
49+
# selector: {"year"=>{:$in=>[2020]}}
50+
# options: {}
51+
# class: Band
52+
# embedded: false>
53+
54+
Note that this stringification does not yet happen for all query construction
55+
paths. For example, ``where`` does not stringify operators in both Mongoid 7.3
56+
and Mongoid 7.2:
57+
58+
.. code-block:: ruby
59+
60+
Band.where(year: {'$in': [2020]})
61+
# =>
62+
# #<Mongoid::Criteria
63+
# selector: {"year"=>{:$in=>[2020]}}
64+
# options: {}
65+
# class: Band
66+
# embedded: false>
67+
68+
It is expected that over time, all operators will stringify the keys in their
69+
arguments.
70+
71+
72+
Condition Combination Using ``$eq``
73+
-----------------------------------
74+
75+
Minor change: when using the ``and`` method on ``Criteria`` objects and
76+
providing multiple conditions on the same field in the same argument to
77+
``and``, conditions may be combined using ``$eq`` instead of ``$and``.
78+
Mongoid 7.3 behavior:
79+
80+
.. code-block:: ruby
81+
82+
Band.and(year: 2020, :year.gt => 1960)
83+
# =>
84+
# #<Mongoid::Criteria
85+
# selector: {"year"=>{"$eq"=>2020, "$gt"=>1960}}
86+
# options: {}
87+
# class: Band
88+
# embedded: false>
89+
90+
Band.and(:year.gt => 1960, year: 2020)
91+
# =>
92+
# #<Mongoid::Criteria
93+
# selector: {"year"=>{"$gt"=>1960, "$eq"=>2020}}
94+
# options: {}
95+
# class: Band
96+
# embedded: false>
97+
98+
Mongoid 7.2 behavior:
99+
100+
.. code-block:: ruby
101+
102+
Band.and(year: 2020, :year.gt => 1960)
103+
# =>
104+
# #<Mongoid::Criteria
105+
# selector: {"year"=>2020, "$and"=>[{"year"=>{"$gt"=>1960}}]}
106+
# options: {}
107+
# class: Band
108+
# embedded: false>
109+
110+
Band.and(:year.gt => 1960, year: 2020)
111+
# =>
112+
# #<Mongoid::Criteria
113+
# selector: {"year"=>{"$gt"=>1960}, "$and"=>[{"year"=>2020}]}
114+
# options: {}
115+
# class: Band
116+
# embedded: false>
117+
118+
This combination is not yet performed for ``where`` and other query methods:
119+
120+
.. code-block:: ruby
121+
122+
Band.where(:year.gt => 1960, year: 2020)
123+
# =>
124+
# #<Mongoid::Criteria
125+
# selector: {"year"=>{"$gt"=>1960}, "$and"=>[{"year"=>2020}]}
126+
# options: {}
127+
# class: Band
128+
# embedded: false>
129+
130+
Tt is expected that in the future other query methods will also favor
131+
using ``$eq`` over ``$and``.
132+
133+
21134
Upgrading to Mongoid 7.2
22135
========================
23136

24-
The following sections describe major changes in Mongoid 7.2.
137+
The following sections describe significant changes in Mongoid 7.2.
25138

26139
Embedded Document Matching
27140
--------------------------
@@ -416,7 +529,7 @@ of the legacy query cache, see :ref:`the query cache documentation <query-cache>
416529
Upgrading to Mongoid 7.1
417530
========================
418531

419-
The following sections describe major changes in Mongoid 7.1.
532+
The following sections describe significant changes in Mongoid 7.1.
420533

421534
Condition Combination
422535
---------------------
@@ -448,6 +561,45 @@ Corresponding Mongoid 7.0 behavior:
448561
# class: Band
449562
# embedded: false>
450563

564+
**Known issue:** When using Ruby 2.6 and lower, when adding multiple conditions
565+
on the same field using the same operator, the operator must be given as a
566+
string, not as a symbol. The following invocations fail:
567+
568+
.. code-block:: ruby
569+
570+
Band.and({year: {'$in': [2020]}}, {year: {'$in': [2020]}})
571+
# Traceback (most recent call last):
572+
# 2: from (irb):10
573+
# 1: from (irb):10:in `rescue in irb_binding'
574+
# NoMethodError (undefined method `start_with?' for :$in:Symbol)
575+
576+
Band.and(year: {'$in': [2020]}).and(year: {'$in': [2020]})
577+
# Traceback (most recent call last):
578+
# 2: from (irb):11
579+
# 1: from (irb):11:in `rescue in irb_binding'
580+
# NoMethodError (undefined method `start_with?' for :$in:Symbol)
581+
582+
Use string keys instead:
583+
584+
.. code-block:: ruby
585+
586+
# Band.and({year: {'$in' => [2020]}}, {year: {'$in' => [2020]}})
587+
# => #<Mongoid::Criteria
588+
# selector: {"year"=>{"$in"=>[2020]}, "$and"=>[{"year"=>{"$in"=>[2020]}}]}
589+
# options: {}
590+
# class: Band
591+
# embedded: false>
592+
593+
Band.and(year: {'$in' => [2020]}).and(year: {'$in' => [2020]})
594+
# => #<Mongoid::Criteria
595+
# selector: {"year"=>{"$in"=>[2020]}, "$and"=>[{"year"=>{"$in"=>[2020]}}]}
596+
# options: {}
597+
# class: Band
598+
# embedded: false>
599+
600+
This issue is rectified in Mongoid 7.3.
601+
602+
451603
Logical Operations
452604
------------------
453605

0 commit comments

Comments
 (0)