-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MONGOID-5411 allow results to be returned as demongoized hashes #5877
Conversation
Jamis, thank you, this is super useful. Please kindly check the following:
|
Good suggestion. I've added a few tests for that here.
I think
There are already tests for this
This is already done, as well. Thanks for the feedback, @johnnyshields! |
I did a couple of simple benchmarks on this, to get some idea of just how much difference this makes. The benchmarks were:
Each benchmark was performed one hundred times, and the median elapsed time captured. Each time was then reported relative to the baseline benchmark. "Raw" results have no typecasting applied to them. "Raw (Typed)" benchmarks are typecast according to the fields declared on the corresponding model (they are "demongoized"). The results:
Memory ComparisonFor a memory benchmark, I used the same operations as above, but for each the median memory usage was reported. The numbers here represent memory usage relative to the baseline benchmark.
ConclusionsReturning just the demongoized hashes makes very little difference in performance for records with no embedded children. The memory profile is significantly better, though. Returning demongoized hashes for records with embedded children hurts both performance and memory usage, if you do not intend to access the embedded children. Returning demongoized hashes is significantly better (both in time and in memory) when querying records with embedded children, when you intend to access those embedded children. |
For benchmarks, try:
One of my main use cases for this is reporting, where I'm getting 1,000,000+ objects and putting them in a CSV. Currently I'm using the |
@johnnyshields -- I've updated my previous comment to include some basic memory profiling. |
Returns the hashes exactly as fetched from the database.
I also decided it was probably worth going the distance, here, and providing completely raw results (not typecast at all) as the default. If you want demongoized hashes, you can specify records = Person.raw(typed: true).to_a |
Mongoid now supports a new
raw
directive when building queries. When present, the resulting query will be returned as hashes, rather than instantiated document models. The hashes will not be "demongoized" -- the values will be returned exactly as provided by the database, directly.If you specify
typed: true
, the hashes will be returned with the values translated from the raw value stored in the database, into the corresponding type defined by thefield
definitions on the model. (That is to say, they will be "demongoized".)For example:
The
typed: true
option will also honor embedded documents, correctly demongoizing the embedded hashes according to their declared types.