Skip to content

Commit d3ee0a2

Browse files
neilshwekyp-mongo
andauthored
MONGOID-4547 Implement #take / #take! for AR feature parity (#5354)
* MONGOID-4547 add new error * MONGOID-4547 implement take/take! * MONGOID-4547 some cleanup * MONGOID-4547 don't use Mongo#first * MONGOID-4547 memory implementation * MONGOID-4547 add none implementation * MONGOID-4547 fix non implementation * MONGOID-4547 add documentation for take method * MONGOID-4547 add take! method docs * MONGOID-4547 docs changes * Apply suggestions from code review Co-authored-by: Oleg Pudeyev <[email protected]> * MONGOID-4547 change param to limit * Apply suggestions from code review Co-authored-by: Oleg Pudeyev <[email protected]> * MONGOID-4547 address comments * MONGOID-4547 fix document_not_found tests Co-authored-by: Oleg Pudeyev <[email protected]>
1 parent 40652ff commit d3ee0a2

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

source/reference/queries.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,32 @@ Mongoid also has some helpful methods on criteria.
13901390
# expands out to "managers.name" in the query:
13911391
Band.all.pluck('managers.n')
13921392

1393+
* - ``Criteria#take``
1394+
1395+
*Get a list of n documents from the database or just one if no parameter
1396+
is provided.*
1397+
1398+
*This method does not apply a sort to the documents, so it can return
1399+
different document(s) than #first and #last.*
1400+
1401+
-
1402+
.. code-block:: ruby
1403+
1404+
Band.take
1405+
Band.take(5)
1406+
1407+
* - ``Criteria#take!``
1408+
1409+
*Get a document from the database or raise an error if none exist.*
1410+
1411+
*This method does not apply a sort to the documents, so it can return
1412+
different document(s) than #first and #last.*
1413+
1414+
-
1415+
.. code-block:: ruby
1416+
1417+
Band.take!
1418+
13931419
* - ``Criteria#tally``
13941420

13951421
*Get a mapping of values to counts for the provided field.*

source/release-notes/mongoid-7.5.txt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,44 @@ return a ``Hash`` when instantiating a new document:
147147
band = Band.first
148148
p band.attributes.class
149149
# => BSON::Document
150+
151+
152+
Implemented ``Criteria#take/take!`` Method
153+
------------------------------------------
154+
155+
Mongoid 7.5 introduces the ``#take`` method which returns a document
156+
or a set of documents from the database without ordering by ``_id``:
157+
158+
.. code:: ruby
159+
160+
class Band
161+
include Mongoid::Document
162+
end
163+
164+
Band.create!
165+
Band.create!
166+
167+
Band.take
168+
# => #<Band _id: 62c835813282a4470c07d530, >
169+
Band.take(2)
170+
# => [ #<Band _id: 62c835813282a4470c07d530, >, #<Band _id: 62c835823282a4470c07d531, > ]
171+
172+
If a parameter is given to ``#take``, an array of documents is returned. If no parameter is
173+
given, a singular document is returned.
174+
175+
The ``#take!`` method functions the same as calling ``#take`` without arguments,
176+
but raises an DocumentNotFound error instead of returning nil if no documents
177+
are found.
178+
179+
.. code:: ruby
180+
181+
Band.take!
182+
# => #<Band _id: 62c835813282a4470c07d530, >
183+
184+
Note that the ``#take/take!`` methods do not apply a sort to the view before
185+
retrieving the documents from the database, and therefore they may return different
186+
results than the ``#first`` and ``#last`` methods. ``#take`` is equivalent to
187+
calling ``#first`` or ``#last`` with the ``{ id_sort: :none }`` option. This
188+
option has been deprecated in Mongoid 7.5 and it is recommended to use ``#take``
189+
instead going forward. Support for the ``:id_sort`` option will be dropped in
190+
Mongoid 8.

0 commit comments

Comments
 (0)