From 71a70fa14e4c0154d1636c6ee9623b8b6bad986e Mon Sep 17 00:00:00 2001 From: rustagir Date: Tue, 25 Mar 2025 13:09:17 -0400 Subject: [PATCH 1/2] DOCSP-44648: return raw results --- .../includes/interact-data/modify-results.rb | 18 +++++++++ source/interact-data/modify-results.txt | 39 +++++++++++++++++++ source/whats-new.txt | 13 +++++++ 3 files changed, 70 insertions(+) diff --git a/source/includes/interact-data/modify-results.rb b/source/includes/interact-data/modify-results.rb index f2da3cfe..1618f737 100644 --- a/source/includes/interact-data/modify-results.rb +++ b/source/includes/interact-data/modify-results.rb @@ -38,3 +38,21 @@ # start-batch Band.batch_size(500) # end-batch + +# start-raw +Band.where(country: 'Argentina').raw +# end-raw + +# start-raw-typed +Band.where(country: 'Argentina').raw(typed: true) +# end-raw-typed + +# start-raw-false +# Retrieve raw results +results = Band.where(members: 4).raw + +# ... Perform actions on results + +# Returns instantiated model objects from raw results +bands = results.raw(false).to_a +# end-raw-false diff --git a/source/interact-data/modify-results.txt b/source/interact-data/modify-results.txt index f4a7af78..bc2a9cbd 100644 --- a/source/interact-data/modify-results.txt +++ b/source/interact-data/modify-results.txt @@ -30,6 +30,8 @@ following actions to modify the way that results appear: - :ref:`mongoid-data-skip-limit` +- :ref:`mongoid-raw-results` + Sample Data ~~~~~~~~~~~ @@ -306,6 +308,43 @@ The following code sets the batch size to ``500``: :language: ruby :dedent: +.. _mongoid-raw-results: + +Return Raw Data +--------------- + +You can return results as raw hashes without creating model instances by +chaining the ``raw`` method to your query. + +The following code retrieves the query results as raw hashes: + +.. literalinclude:: /includes/interact-data/modify-results.rb + :start-after: start-raw + :end-before: end-raw + :language: ruby + :dedent: + +By default, the ``raw`` method returns documents exactly as {+odm+} +receives them from the database. You can optionally cast fields in your +result hashes to the types from your model field declarations by setting +the ``typed`` option to ``true`` in the ``raw`` method: + +.. literalinclude:: /includes/interact-data/modify-results.rb + :start-after: start-raw-typed + :end-before: end-raw-typed + :language: ruby + :dedent: + +If you perform a query that returns raw results and later want to +convert these results into models, you can call ``raw(false)`` on the +results: + +.. literalinclude:: /includes/interact-data/modify-results.rb + :start-after: start-raw-false + :end-before: end-raw-false + :language: ruby + :dedent: + Additional Information ---------------------- diff --git a/source/whats-new.txt b/source/whats-new.txt index 5ca00677..ddebc8fa 100644 --- a/source/whats-new.txt +++ b/source/whats-new.txt @@ -12,11 +12,24 @@ What's New Learn what's new in: +* :ref:`Version 9.1 ` * :ref:`Version 9.0 ` To view a list of releases and detailed release notes, see {+odm+} :github:`Releases ` on GitHub. +.. _mongoid-version-9.1: + +What's New in 9.1 +----------------- + +The 9.1 release includes the following new features, improvements, and +fixes: + +- Adds the ``Criteria#raw`` method that allows you to retrieve results + as raw hashes. To learn more, see the :ref:`mongoid-raw-results` + section of the Modify Query Results guide. + .. _mongoid-version-9.0: What's New in 9.0 From ca54d3e2b5321a85ca6695b6dcb7fb09283c824b Mon Sep 17 00:00:00 2001 From: rustagir Date: Tue, 25 Mar 2025 13:54:31 -0400 Subject: [PATCH 2/2] small fix --- source/includes/interact-data/modify-results.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/includes/interact-data/modify-results.rb b/source/includes/interact-data/modify-results.rb index 1618f737..3b8fec0b 100644 --- a/source/includes/interact-data/modify-results.rb +++ b/source/includes/interact-data/modify-results.rb @@ -48,7 +48,7 @@ # end-raw-typed # start-raw-false -# Retrieve raw results +# Retrieves raw results results = Band.where(members: 4).raw # ... Perform actions on results