Skip to content

Commit baad046

Browse files
neilshwekyp-mongo
andauthored
MONGOID-5330 Operator methods overwrite previous conditions (#5263)
* MONGOID-5330 add failing test * MONGOID-5330 pend test * MONGOID-5330 make gt use and * MONGOID-5330 change hard coded value * MONGOID-5330 fix tests * MONGOID-5330 add feature flag * MONGOID-5330 use the feature flag * MONGOID-5330 add testing and fix eq * MONGOID-5330 change op map * MONGOID-5330 fix elemMatch * Update lib/mongoid/criteria/queryable/mergeable.rb * Update lib/mongoid/criteria/queryable/mergeable.rb * Update lib/mongoid/criteria/queryable/mergeable.rb * MONGOID-5330 make this feature flag in 7.5 * MONGOID-5330 remove extra spacing * MONGOID-5330 update docs to update default * MONGOID-5330 update tests after switching defaults * MONGOID-5330 update docs with note about strings vs symbols * MONGOID-5330 update test * MONGOID-5330 change feature flag to overwrite Co-authored-by: Oleg Pudeyev <[email protected]>
1 parent 81508f3 commit baad046

File tree

3 files changed

+97
-8
lines changed

3 files changed

+97
-8
lines changed

source/reference/configuration.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,12 @@ for details on driver options.
395395
# (default: false)
396396
#object_id_as_json_oid: true
397397

398+
# When chaining the same operators that use the same field, setting this
399+
# feature flag to false will cause those operators to be combined using an
400+
# and. Setting this feature flag to true will cause the later chained
401+
# operators to overwrite the earlier ones. (default: false)
402+
#overwrite_chained_operators: false
403+
398404
# Preload all models in development, needed when models use
399405
# inheritance. (default: false)
400406
preload_models: false

source/release-notes/mongoid-7.5.txt

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
***********
2+
Mongoid 7.5
3+
***********
4+
5+
.. default-domain:: mongodb
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
This page describes significant changes and improvements in Mongoid 7.5.
14+
The complete list of releases is available `on GitHub
15+
<https://github.com/mongodb/mongoid/releases>`_ and `in JIRA
16+
<https://jira.mongodb.org/projects/MONGOID?selectedItem=com.atlassian.jira.jira-projects-plugin:release-page>`_;
17+
please consult GitHub releases for detailed release notes and JIRA for
18+
the complete list of issues fixed in each release, including bug fixes.
19+
20+
21+
``Document#to_a`` deprecated
22+
````````````````````````````
23+
24+
The ``Document#to_a`` method is deprecated in Mongoid 7.5.
25+
26+
27+
Combine Chained Operators Using ``and`` Instead of ``override``
28+
```````````````````````````````````````````````````````````````
29+
30+
Mongoid 7.5 with the ``Mongoid.overwrite_chained_operators`` option set to ``false``
31+
will combine conditions that use the same operator and field using an ``and``.
32+
For example, in the following query:
33+
34+
.. code-block:: ruby
35+
36+
Band.ne(name: "The Rolling Stones").ne(name: "The Beatles")
37+
38+
Mongoid 7.5 with the ``Mongoid.overwrite_chained_operators`` option set to ``false``
39+
will generate the following criteria:
40+
41+
.. code-block:: ruby
42+
43+
#<Mongoid::Criteria
44+
selector: {"name"=>{"$ne"=>"The Rolling Stones"}, "$and"=>[{"name"=>{"$ne"=>"The Beatles"}}]}
45+
options: {}
46+
class: Band
47+
embedded: false>
48+
49+
In Mongoid 7.4 and earlier, and in 7.5 with the ``Mongoid.overwrite_chained_operators``
50+
option set to ``true``, the following criteria would be generated instead which
51+
overwrites the first condition:
52+
53+
.. code-block:: ruby
54+
55+
#<Mongoid::Criteria
56+
selector: {"name"=>{"$ne"=>"The Beatles"}}
57+
options: {}
58+
class: Band
59+
embedded: false>
60+
61+
The following functions are affected by this change:
62+
63+
- ``eq``
64+
- ``elem_match``
65+
- ``gt``
66+
- ``gte``
67+
- ``lt``
68+
- ``lte``
69+
- ``mod``
70+
- ``ne``
71+
- ``near``
72+
- ``near_sphere``
73+
74+
.. note::
75+
76+
In Mongoid 7.5 with the ``Mongoid.overwrite_chained_operators`` option set to
77+
``false``, nested keys in the generated selector will always be strings,
78+
whereas in Mongoid 7.4 and earlier, and in 7.5 with the
79+
``Mongoid.overwrite_chained_operators`` option set to ``true``, nested keys in
80+
the selector can be strings or symbols, depending on what was passed to the
81+
operator.
82+

source/release-notes/mongoid-8.0.txt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Default Option Values Changed
3131
**Breaking change:** The following options have had their default values
3232
changed in Mongoid 8.0:
3333

34+
- ``:overwrite_chained_operators`` => ``false``
3435
- ``:broken_aggregables`` => ``false``
3536
- ``:broken_alias_handling`` => ``false``
3637
- ``:broken_and`` => ``false``
@@ -161,20 +162,20 @@ Mongoid 8.0 behavior:
161162

162163
.. code-block:: ruby
163164

164-
Band.any_of({name: 'Rolling Stone'}, {founded: 1990}).
165+
Band.any_of({name: 'The Rolling Stones'}, {founded: 1990}).
165166
any_of({members: 2}, {last_tour: 1995})
166167
# =>
167168
# #<Mongoid::Criteria
168-
# selector: {"$or"=>[{"name"=>"Rolling Stone"}, {"founded"=>1990}],
169+
# selector: {"$or"=>[{"name"=>"The Rolling Stones"}, {"founded"=>1990}],
169170
# "$and"=>[{"$or"=>[{"members"=>2}, {"last_tour"=>1995}]}]}
170171
# options: {}
171172
# class: Band
172173
# embedded: false>
173174

174-
Band.any_of({name: 'Rolling Stone'}, {founded: 1990}).any_of({members: 2})
175+
Band.any_of({name: 'The Rolling Stones'}, {founded: 1990}).any_of({members: 2})
175176
# =>
176177
# #<Mongoid::Criteria
177-
# selector: {"$or"=>[{"name"=>"Rolling Stone"}, {"founded"=>1990}], "members"=>2}
178+
# selector: {"$or"=>[{"name"=>"The Rolling Stones"}, {"founded"=>1990}], "members"=>2}
178179
# options: {}
179180
# class: Band
180181
# embedded: false>
@@ -183,20 +184,20 @@ Mongoid 7 behavior:
183184

184185
.. code-block:: ruby
185186

186-
Band.any_of({name: 'Rolling Stone'}, {founded: 1990}).
187+
Band.any_of({name: 'The Rolling Stones'}, {founded: 1990}).
187188
any_of({members: 2}, {last_tour: 1995})
188189
# =>
189190
# #<Mongoid::Criteria
190-
# selector: {"$or"=>[{"name"=>"Rolling Stone"}, {"founded"=>1990},
191+
# selector: {"$or"=>[{"name"=>"The Rolling Stones"}, {"founded"=>1990},
191192
# {"members"=>2}, {"last_tour"=>1995}]}
192193
# options: {}
193194
# class: Band
194195
# embedded: false>
195196

196-
Band.any_of({name: 'Rolling Stone'}, {founded: 1990}).any_of({members: 2})
197+
Band.any_of({name: 'The Rolling Stones'}, {founded: 1990}).any_of({members: 2})
197198
# =>
198199
# #<Mongoid::Criteria
199-
# selector: {"$or"=>[{"name"=>"Rolling Stone"}, {"founded"=>1990}], "members"=>2}
200+
# selector: {"$or"=>[{"name"=>"The Rolling Stones"}, {"founded"=>1990}], "members"=>2}
200201
# options: {}
201202
# class: Band
202203
# embedded: false>

0 commit comments

Comments
 (0)