@@ -1249,6 +1249,29 @@ Mongoid also has some helpful methods on criteria.
1249
1249
Band.exists?
1250
1250
Band.where(name: "Photek").exists?
1251
1251
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
+
1252
1275
* - ``Criteria#find_by``
1253
1276
1254
1277
*Find a document by the provided attributes. If not found,
@@ -1314,6 +1337,20 @@ Mongoid also has some helpful methods on criteria.
1314
1337
Band.where(:members.with_size => 3).last
1315
1338
Band.first(2)
1316
1339
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
+
1317
1354
* - ``Criteria#first_or_create``
1318
1355
1319
1356
*Find the first document by the provided attributes, and if not found
@@ -1361,6 +1398,29 @@ Mongoid also has some helpful methods on criteria.
1361
1398
# MongoDB 4.2 and lower
1362
1399
Band.for_js("this.name = param", param: "Tool")
1363
1400
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
+
1364
1424
* - ``Criteria#length|size``
1365
1425
1366
1426
*Same as count but caches subsequent calls to the database*
@@ -1427,6 +1487,52 @@ Mongoid also has some helpful methods on criteria.
1427
1487
Band.all.pluck(:name, :likes)
1428
1488
#=> [ ["Daft Punk", 342], ["Aphex Twin", 98], ["Ween", 227] ]
1429
1489
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
+
1430
1536
* - ``Criteria#take``
1431
1537
1432
1538
*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.
1474
1580
# expands out to "managers.name" in the query:
1475
1581
Band.all.tally('managers.n')
1476
1582
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
+
1477
1629
1478
1630
Eager Loading
1479
1631
=============
0 commit comments