Skip to content

Commit 7a555df

Browse files
authored
MONGOID-5418 AR Feature Parity first!/last!/second etc. (#5396)
* MONGOID-5418 add test for first-fifth(!) * MONGOID-5418 add rest of the methods + findable tests * MONGOID-5418 add memory methods * MONGOID-5418 add none methods * MONGOID-5418 add documentation * MONGOID-5418 add release notes * MONGOID-5418 fix query docs * MONGOID-5418 fix query cache tests
1 parent f273acd commit 7a555df

File tree

10 files changed

+2229
-129
lines changed

10 files changed

+2229
-129
lines changed

docs/reference/queries.txt

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,29 @@ Mongoid also has some helpful methods on criteria.
12491249
Band.exists?
12501250
Band.where(name: "Photek").exists?
12511251

1252+
* - ``Criteria#fifth``
1253+
1254+
*Get the fifth document for the given criteria.*
1255+
1256+
*This method automatically adds a sort on _id if no sort is given.*
1257+
1258+
-
1259+
.. code-block:: ruby
1260+
1261+
Band.fifth
1262+
1263+
* - ``Criteria#fifth!``
1264+
1265+
*Get the fifth document for the given criteria, or raise an error if
1266+
none exist.*
1267+
1268+
*This method automatically adds a sort on _id if no sort is given.*
1269+
1270+
-
1271+
.. code-block:: ruby
1272+
1273+
Band.fifth!
1274+
12521275
* - ``Criteria#find_by``
12531276

12541277
*Find a document by the provided attributes. If not found,
@@ -1314,6 +1337,20 @@ Mongoid also has some helpful methods on criteria.
13141337
Band.where(:members.with_size => 3).last
13151338
Band.first(2)
13161339

1340+
* - ``Criteria#first!|last!``
1341+
1342+
*Finds a single document given the provided criteria, or raises an error
1343+
if none are found. This method automatically adds a sort on _id if no
1344+
sort is given. This can cause performance issues, so if the sort is
1345+
undesirable, Criteria#take! can be used instead.*
1346+
1347+
-
1348+
.. code-block:: ruby
1349+
1350+
Band.first!
1351+
Band.where(:members.with_size => 3).first!
1352+
Band.where(:members.with_size => 3).last!
1353+
13171354
* - ``Criteria#first_or_create``
13181355

13191356
*Find the first document by the provided attributes, and if not found
@@ -1361,6 +1398,29 @@ Mongoid also has some helpful methods on criteria.
13611398
# MongoDB 4.2 and lower
13621399
Band.for_js("this.name = param", param: "Tool")
13631400

1401+
* - ``Criteria#fourth``
1402+
1403+
*Get the fourth document for the given criteria.*
1404+
1405+
*This method automatically adds a sort on _id if no sort is given.*
1406+
1407+
-
1408+
.. code-block:: ruby
1409+
1410+
Band.fourth
1411+
1412+
* - ``Criteria#fourth!``
1413+
1414+
*Get the fourth document for the given criteria, or raise an error if
1415+
none exist.*
1416+
1417+
*This method automatically adds a sort on _id if no sort is given.*
1418+
1419+
-
1420+
.. code-block:: ruby
1421+
1422+
Band.fourth!
1423+
13641424
* - ``Criteria#length|size``
13651425

13661426
*Same as count but caches subsequent calls to the database*
@@ -1427,6 +1487,52 @@ Mongoid also has some helpful methods on criteria.
14271487
Band.all.pluck(:name, :likes)
14281488
#=> [ ["Daft Punk", 342], ["Aphex Twin", 98], ["Ween", 227] ]
14291489

1490+
* - ``Criteria#second``
1491+
1492+
*Get the second document for the given criteria.*
1493+
1494+
*This method automatically adds a sort on _id if no sort is given.*
1495+
1496+
-
1497+
.. code-block:: ruby
1498+
1499+
Band.second
1500+
1501+
* - ``Criteria#second!``
1502+
1503+
*Get the second document for the given criteria, or raise an error if
1504+
none exist.*
1505+
1506+
*This method automatically adds a sort on _id if no sort is given.*
1507+
1508+
-
1509+
.. code-block:: ruby
1510+
1511+
Band.second!
1512+
1513+
* - ``Criteria#second_to_last``
1514+
1515+
*Get the second to last document for the given criteria.*
1516+
1517+
*This method automatically adds a sort on _id if no sort is given.*
1518+
1519+
-
1520+
.. code-block:: ruby
1521+
1522+
Band.second_to_last
1523+
1524+
* - ``Criteria#second_to_last!``
1525+
1526+
*Get the second to last document for the given criteria, or raise an
1527+
error if none exist.*
1528+
1529+
*This method automatically adds a sort on _id if no sort is given.*
1530+
1531+
-
1532+
.. code-block:: ruby
1533+
1534+
Band.second_to_last!
1535+
14301536
* - ``Criteria#take``
14311537

14321538
*Get a list of n documents from the database or just one if no parameter
@@ -1474,6 +1580,52 @@ Mongoid also has some helpful methods on criteria.
14741580
# expands out to "managers.name" in the query:
14751581
Band.all.tally('managers.n')
14761582

1583+
* - ``Criteria#third``
1584+
1585+
*Get the third document for the given criteria.*
1586+
1587+
*This method automatically adds a sort on _id if no sort is given.*
1588+
1589+
-
1590+
.. code-block:: ruby
1591+
1592+
Band.third
1593+
1594+
* - ``Criteria#third!``
1595+
1596+
*Get the third document for the given criteria, or raise an error if
1597+
none exist.*
1598+
1599+
*This method automatically adds a sort on _id if no sort is given.*
1600+
1601+
-
1602+
.. code-block:: ruby
1603+
1604+
Band.third!
1605+
1606+
* - ``Criteria#third_to_last``
1607+
1608+
*Get the third to last document for the given criteria.*
1609+
1610+
*This method automatically adds a sort on _id if no sort is given.*
1611+
1612+
-
1613+
.. code-block:: ruby
1614+
1615+
Band.third_to_last
1616+
1617+
* - ``Criteria#third_to_last!``
1618+
1619+
*Get the third to last document for the given criteria, or raise an
1620+
error if none exist.*
1621+
1622+
*This method automatically adds a sort on _id if no sort is given.*
1623+
1624+
-
1625+
.. code-block:: ruby
1626+
1627+
Band.third_to_last!
1628+
14771629

14781630
Eager Loading
14791631
=============

docs/release-notes/mongoid-8.1.txt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
***********
2+
Mongoid 8.1
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 8.1.
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+
Added ``Mongoid::Criteria`` finder methods
22+
------------------------------------------
23+
24+
Mongoid 8.1 implements several finder methods on ``Mongoid::Criteria``:
25+
26+
- ``first!``
27+
- ``last!``
28+
- ``second/second!``
29+
- ``third/third!``
30+
- ``fourth/fourth!``
31+
- ``fifth/fifth!``
32+
- ``second_to_last/second_to_last!``
33+
- ``third_to_last/third_to_last!``
34+
35+
When no documents are found, methods without a bang (!) return nil, and methods
36+
with a bang (!) raise an error:
37+
38+
.. code:: ruby
39+
40+
Band.none.first
41+
# => nil
42+
43+
Band.none.first!
44+
# => raise Mongoid::Errors::DocumentNotFound

0 commit comments

Comments
 (0)