Skip to content

Commit d6b504a

Browse files
p-mongop
andauthored
MONGOID-4862 Make any_of behave like or but not disjunct the receiver (#4741)
* MONGOID-4862 Make any_of behave like or but not disjunct the receiver * grammar fix Co-authored-by: Oleg Pudeyev <oleg@bsdpower.com>
1 parent 8ab19b1 commit d6b504a

File tree

1 file changed

+44
-13
lines changed

1 file changed

+44
-13
lines changed

source/tutorials/mongoid-queries.txt

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,11 @@ The following calls all produce the same query conditions:
365365
Operator Combinations
366366
`````````````````````
367367

368-
As of Mongoid 7.1, logical operators have been changed to have the
369-
the same semantics as `those of ActiveRecord
368+
As of Mongoid 7.1, logical operators (``and``, ``or``, ``nor`` and ``not``)
369+
have been changed to have the the same semantics as `those of ActiveRecord
370370
<https://guides.rubyonrails.org/active_record_querying.html>`_.
371+
To obtain the semantics of ``or`` as it behaved in Mongoid 7.0 and earlier,
372+
use ``any_of`` which is described below.
371373

372374
When conditions are specified on the same field multiple times, all
373375
conditions are added to the criteria:
@@ -380,11 +382,12 @@ conditions are added to the criteria:
380382
Band.where(name: 1).or(name: 2).selector
381383
# => {"$or"=>[{"name"=>"1"}, {"name"=>"2"}]}
382384

383-
``nor`` and ``not`` behave similarly, with ``not`` producing different query
384-
shapes as described later.
385+
``any_of``, ``nor`` and ``not`` behave similarly, with ``not`` producing
386+
different query shapes as described below.
385387

386-
When a logical operator is used, it operates on the criteria built up to
387-
that point and its argument. ``where`` has the same meaning as ``and``:
388+
When ``and``, ``or`` and ``nor`` logical operators are used, they
389+
operate on the criteria built up to that point and its argument.
390+
``where`` has the same meaning as ``and``:
388391

389392
.. code-block:: ruby
390393

@@ -434,21 +437,49 @@ the same field, depending on which form of ``and`` was used.
434437
``or``/``nor`` Behavior
435438
```````````````````````
436439

437-
``or`` and ``nor`` always combine conditions with ``$or`` and ``$nor``
438-
MongoDB operators, respectively. If the only condition on the receiving
439-
criteria is another ``or``/``nor``, new conditions are added to the
440-
existing list, otherwise a new top-level ``$or``/``$nor`` operator is created.
440+
``or`` and ``nor`` produce ``$or`` and ``$nor`` MongoDB operators, respectively,
441+
using the receiver and all of the arguments as operands. For example:
441442

442443
.. code-block:: ruby
443444

444-
Band.where(name: /Best/).or(name: 'Astral Projection').
445-
or(Band.where(label: /Records/)).selector
446-
# => {"$or"=>[{"name"=>/Best/}, {"name"=>"Astral Projection"}, {"label"=>/Records/}]}
445+
Band.where(name: /Best/).or(name: 'Astral Projection')
446+
# => {"$or"=>[{"name"=>/Best/}, {"name"=>"Astral Projection"}]}
447447

448448
Band.where(name: /Best/).and(name: 'Astral Projection').
449449
or(Band.where(label: /Records/)).and(label: 'Trust').selector
450450
# => {"$or"=>[{"name"=>/Best/, "$and"=>[{"name"=>"Astral Projection"}]}, {"label"=>/Records/}], "label"=>"Trust"}
451451

452+
If the only condition on the receiver is another ``or``/``nor``, the new
453+
conditions are added to the existing list:
454+
455+
.. code-block:: ruby
456+
457+
Band.where(name: /Best/).or(name: 'Astral Projection').
458+
or(Band.where(label: /Records/)).selector
459+
# => {"$or"=>[{"name"=>/Best/}, {"name"=>"Astral Projection"}, {"label"=>/Records/}]}
460+
461+
Use ``any_of`` to add a disjunction to a Criteria object while maintaining
462+
all of the conditions built up so far as they are.
463+
464+
465+
``any_of`` Behavior
466+
```````````````````
467+
468+
``any_of`` adds a disjunction built from its arguments to the existing
469+
conditions in the criteria. For example:
470+
471+
.. code-block:: ruby
472+
473+
Band.where(label: /Trust/).any_of({name: 'Astral Projection'}, {name: /Best/})
474+
# => {"label"=>/Trust/, "$or"=>[{"name"=>"Astral Projection"}, {"name"=>/Best/}]}
475+
476+
The conditions are hoisted to the top level if possible:
477+
478+
.. code-block:: ruby
479+
480+
Band.where(label: /Trust/).any_of({name: 'Astral Projection'})
481+
# => {"label"=>/Trust/, "name"=>"Astral Projection"}
482+
452483

453484
``not`` Behavior
454485
````````````````

0 commit comments

Comments
 (0)