Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-5411

Return query execution results as demongoized hashes without instantiating models

    • Type: Icon: New Feature New Feature
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 9.1.0
    • Affects Version/s: None
    • Component/s: None
    • None
    • Fully Compatible
    • Needed
    • Hide

      1. What would you like to communicate to the user about this feature?

      Mongoid now lets you return query results as raw hashes. You can also optionally cast results to the types declared in your models, but by default, the raw hashes are returned exactly as received from the database.

      To receive query results as raw hashes, add the `#raw` method to your query:

       # find all matching Person records as raw hashes
      results = Person.where(...).raw.to_a
      
       # find all matching Person records as raw hashes, with types cast per the field declarations
      results = Person.where(...).raw(typed: true).to_a
      

      If you have a query that enabled raw results, and you later want to change it to return instantiated models, pass `false` to `#raw`:

      # set up the query with raw results
      results = Person.where(...).raw
      
      # do something with that query object
      # ...
      
      # then, later, switch it back so it returns instantiated model objects
      people = results.raw(false).to_a
      

      2. Would you like the user to see examples of the syntax and/or executable code and its output?

      Yes, please, per the examples above.

      3. Which versions of the driver/connector does this apply to?

      This will apply to Mongoid 9.1.0 (which is not yet released).

      Show
      1. What would you like to communicate to the user about this feature? Mongoid now lets you return query results as raw hashes. You can also optionally cast results to the types declared in your models, but by default, the raw hashes are returned exactly as received from the database. To receive query results as raw hashes, add the `#raw` method to your query: # find all matching Person records as raw hashes results = Person.where(...).raw.to_a # find all matching Person records as raw hashes, with types cast per the field declarations results = Person.where(...).raw(typed: true ).to_a If you have a query that enabled raw results, and you later want to change it to return instantiated models, pass `false` to `#raw`: # set up the query with raw results results = Person.where(...).raw # do something with that query object # ... # then , later, switch it back so it returns instantiated model objects people = results.raw( false ).to_a 2. Would you like the user to see examples of the syntax and/or executable code and its output? Yes, please, per the examples above. 3. Which versions of the driver/connector does this apply to? This will apply to Mongoid 9.1.0 (which is not yet released).

      In https://jira.mongodb.org/browse/MONGOID-5401 is is requested that tally takes multiple arguments. While this could be theoretically useful, I think it is too specific of an optimization to be part of Mongoid itself.

      To better support use cases where the application just wants to return raw data from the database without creating model instances, we could add a method to Criteria to return the results of query execution as BSON::Document instances without instantiating models.

      This is essentially an inverse of MONGOID-5347.

      Note that this ticket won't help specifically with the tally request in MONGOID-5401, because tally uses the aggregation pipeline. In that case the user would need to construct their own aggregation pipeline.

      The result set should be iterated in batches
      It should be possible to access each document in a convenient manner, e.g. like `each`

      Consider de-mongoizing the return values

            Assignee:
            jamis.buck@mongodb.com Jamis Buck
            Reporter:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: