@@ -851,7 +851,7 @@ The opposite of ``only``, ``without`` causes the specified fields to be omitted:
851
851
.. code-block:: ruby
852
852
853
853
Band.without(:name)
854
- # =>
854
+ # =>
855
855
# #<Mongoid::Criteria
856
856
# selector: {}
857
857
# options: {:fields=>{"name"=>0}}
@@ -864,15 +864,15 @@ as its ``id`` alias) cannot be omitted via ``without``:
864
864
.. code-block:: ruby
865
865
866
866
Band.without(:name, :id)
867
- # =>
867
+ # =>
868
868
# #<Mongoid::Criteria
869
869
# selector: {}
870
870
# options: {:fields=>{"name"=>0}}
871
871
# class: Band
872
872
# embedded: false>
873
873
874
874
Band.without(:name, :_id)
875
- # =>
875
+ # =>
876
876
# #<Mongoid::Criteria
877
877
# selector: {}
878
878
# options: {:fields=>{"name"=>0}}
@@ -1191,10 +1191,10 @@ Mongoid also has some helpful methods on criteria.
1191
1191
1192
1192
*Get a list of distinct values for a single field. Note this will always hit
1193
1193
the database for the distinct values.*
1194
-
1194
+
1195
1195
*This method accepts the dot notation, thus permitting referencing
1196
1196
fields in embedded associations.*
1197
-
1197
+
1198
1198
*This method respects :ref:`field aliases <field-aliases>`,
1199
1199
including those defined in embedded documents.*
1200
1200
@@ -1206,14 +1206,14 @@ Mongoid also has some helpful methods on criteria.
1206
1206
distinct(:name)
1207
1207
1208
1208
Band.distinct('cities.name')
1209
-
1209
+
1210
1210
# Assuming an aliased field:
1211
1211
class Manager
1212
1212
include Mongoid::Document
1213
1213
embedded_in :band
1214
1214
field :name, as: :n
1215
1215
end
1216
-
1216
+
1217
1217
# Expands out to "managers.name" in the query:
1218
1218
Band.distinct('managers.n')
1219
1219
@@ -1362,10 +1362,10 @@ Mongoid also has some helpful methods on criteria.
1362
1362
1363
1363
*Get all the values for the provided field.
1364
1364
Returns nil for unset fields and for non-existent fields.*
1365
-
1365
+
1366
1366
*This method accepts the dot notation, thus permitting referencing
1367
1367
fields in embedded associations.*
1368
-
1368
+
1369
1369
*This method respects :ref:`field aliases <field-aliases>`,
1370
1370
including those defined in embedded documents.*
1371
1371
@@ -1457,10 +1457,10 @@ field, a ``Date`` field and an implicit ``Object`` field, and also
1457
1457
intentionally does not define a field called ``deregistered_at``:
1458
1458
1459
1459
.. code-block:: ruby
1460
-
1460
+
1461
1461
class Voter
1462
1462
include Mongoid::Document
1463
-
1463
+
1464
1464
field :born_on, type: Date
1465
1465
field :registered_at, type: Time
1466
1466
field :voted_at
@@ -1473,7 +1473,7 @@ values, respectively, are straightforward:
1473
1473
1474
1474
Voter.where(born_on: Date.today).selector
1475
1475
# => {"born_on"=>2020-12-18 00:00:00 UTC}
1476
-
1476
+
1477
1477
Voter.where(registered_at: Time.now).selector
1478
1478
# => {"registered_at"=>2020-12-19 04:33:36.939788067 UTC}
1479
1479
@@ -1484,13 +1484,13 @@ in all possible scenarios:
1484
1484
1485
1485
Voter.where(born_on: Date.today).selector
1486
1486
# => {"born_on"=>2020-12-18 00:00:00 UTC}
1487
-
1487
+
1488
1488
Voter.where(registered_at: Date.today).selector
1489
1489
# => {"registered_at"=>2020-12-18 00:00:00 -0500}
1490
-
1490
+
1491
1491
Voter.where(voted_at: Date.today).selector
1492
1492
# => {"voted_at"=>Fri, 18 Dec 2020}
1493
-
1493
+
1494
1494
Voter.where(deregistered_at: Date.today).selector
1495
1495
# => {"deregistered_at"=>2020-12-18 00:00:00 UTC}
1496
1496
@@ -1715,38 +1715,38 @@ that would affect its visibility within the scoped association.
1715
1715
1716
1716
class Band
1717
1717
include Mongoid::Document
1718
-
1718
+
1719
1719
field :name
1720
1720
field :active
1721
1721
field :touring
1722
-
1722
+
1723
1723
default_scope ->{ where(active: true) }
1724
1724
end
1725
-
1725
+
1726
1726
Band.where(name: 'Infected Mushroom')
1727
- # =>
1727
+ # =>
1728
1728
# #<Mongoid::Criteria
1729
1729
# selector: {"active"=>true, "name"=>"Infected Mushroom"}
1730
1730
# options: {}
1731
1731
# class: Band
1732
1732
# embedded: false>
1733
-
1733
+
1734
1734
Band.where(name: 'Infected Mushroom').or(touring: true)
1735
- # =>
1735
+ # =>
1736
1736
# #<Mongoid::Criteria
1737
1737
# selector: {"$or"=>[{"active"=>true, "name"=>"Infected Mushroom"}, {"touring"=>true}]}
1738
1738
# options: {}
1739
1739
# class: Band
1740
1740
# embedded: false>
1741
-
1741
+
1742
1742
Band.or(touring: true)
1743
- # =>
1743
+ # =>
1744
1744
# #<Mongoid::Criteria
1745
1745
# selector: {"$or"=>[{"active"=>true}, {"touring"=>true}]}
1746
1746
# options: {}
1747
1747
# class: Band
1748
1748
# embedded: false>
1749
-
1749
+
1750
1750
In the last example, you might expect the two conditions
1751
1751
(``active: true`` and ``touring: true``) to be combined with an ``$and``,
1752
1752
but because the ``Band`` class already has the scope applied to it,
@@ -1772,15 +1772,22 @@ at runtime:
1772
1772
criteria = Band.with_scope(Band.english) do
1773
1773
Band.all
1774
1774
end
1775
-
1775
+
1776
1776
criteria
1777
- # =>
1777
+ # =>
1778
1778
# #<Mongoid::Criteria
1779
1779
# selector: {"country"=>"England"}
1780
1780
# options: {}
1781
1781
# class: Band
1782
1782
# embedded: false>
1783
1783
1784
+ .. note::
1785
+
1786
+ In Mongoid versions 7.3.3 and earlier, there is a bug in ``with_scope``
1787
+ where the previous scope is lost after the function returns. Set the
1788
+ ``Mongoid::restore_previous_scope`` global flag to true in order to get the
1789
+ correct functionality.
1790
+
1784
1791
1785
1792
Class Methods
1786
1793
-------------
0 commit comments