Skip to content

Commit 33010de

Browse files
authored
DOCSP-44648: return raw results (#118)
* DOCSP-44648: return raw results * small fix
1 parent 948f41e commit 33010de

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

source/includes/interact-data/modify-results.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,21 @@
3838
# start-batch
3939
Band.batch_size(500)
4040
# end-batch
41+
42+
# start-raw
43+
Band.where(country: 'Argentina').raw
44+
# end-raw
45+
46+
# start-raw-typed
47+
Band.where(country: 'Argentina').raw(typed: true)
48+
# end-raw-typed
49+
50+
# start-raw-false
51+
# Retrieves raw results
52+
results = Band.where(members: 4).raw
53+
54+
# ... Perform actions on results
55+
56+
# Returns instantiated model objects from raw results
57+
bands = results.raw(false).to_a
58+
# end-raw-false

source/interact-data/modify-results.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ following actions to modify the way that results appear:
3030

3131
- :ref:`mongoid-data-skip-limit`
3232

33+
- :ref:`mongoid-raw-results`
34+
3335
Sample Data
3436
~~~~~~~~~~~
3537

@@ -306,6 +308,43 @@ The following code sets the batch size to ``500``:
306308
:language: ruby
307309
:dedent:
308310

311+
.. _mongoid-raw-results:
312+
313+
Return Raw Data
314+
---------------
315+
316+
You can return results as raw hashes without creating model instances by
317+
chaining the ``raw`` method to your query.
318+
319+
The following code retrieves the query results as raw hashes:
320+
321+
.. literalinclude:: /includes/interact-data/modify-results.rb
322+
:start-after: start-raw
323+
:end-before: end-raw
324+
:language: ruby
325+
:dedent:
326+
327+
By default, the ``raw`` method returns documents exactly as {+odm+}
328+
receives them from the database. You can optionally cast fields in your
329+
result hashes to the types from your model field declarations by setting
330+
the ``typed`` option to ``true`` in the ``raw`` method:
331+
332+
.. literalinclude:: /includes/interact-data/modify-results.rb
333+
:start-after: start-raw-typed
334+
:end-before: end-raw-typed
335+
:language: ruby
336+
:dedent:
337+
338+
If you perform a query that returns raw results and later want to
339+
convert these results into models, you can call ``raw(false)`` on the
340+
results:
341+
342+
.. literalinclude:: /includes/interact-data/modify-results.rb
343+
:start-after: start-raw-false
344+
:end-before: end-raw-false
345+
:language: ruby
346+
:dedent:
347+
309348
Additional Information
310349
----------------------
311350

source/whats-new.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,24 @@ What's New
1212

1313
Learn what's new in:
1414

15+
* :ref:`Version 9.1 <mongoid-version-9.1>`
1516
* :ref:`Version 9.0 <mongoid-version-9.0>`
1617

1718
To view a list of releases and detailed release notes, see {+odm+}
1819
:github:`Releases <mongodb/mongoid/releases>` on GitHub.
1920

21+
.. _mongoid-version-9.1:
22+
23+
What's New in 9.1
24+
-----------------
25+
26+
The 9.1 release includes the following new features, improvements, and
27+
fixes:
28+
29+
- Adds the ``Criteria#raw`` method that allows you to retrieve results
30+
as raw hashes. To learn more, see the :ref:`mongoid-raw-results`
31+
section of the Modify Query Results guide.
32+
2033
.. _mongoid-version-9.0:
2134

2235
What's New in 9.0

0 commit comments

Comments
 (0)