-
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-5151 Respect aliased fields in pluck/distinct by having Document.database_field_name recursively consider embedded docs #5047
MONGOID-5151 Respect aliased fields in pluck/distinct by having Document.database_field_name recursively consider embedded docs #5047
Conversation
database_field_name does not currently handle "." delimiter char. As an improvement, it should consider embedded doc "store_as" and embedded field aliases. This is a precursor to further improvements on "distinct" and "pluck" methods.
Hi Johnny. Are you sure the ticket was created? I don't see any new tickets from you just now. |
I'm not sure you are aware, as of MongoDB 5.0 dots and dollars in field names are now explicitly supported. So, if a user wants to store in a field named Because of this a change to how dots are treated needs to be carefully considered. Can you add a user-facing example of what you are proposing the behavior to be? It doesn't need to be a test case, just something we can refer to for discussion purposes. |
@p-mongo sure: Person.where('phones.number' => '+12061112222')
Person.distinct('phones.number') This works currently, if there are no aliases.
By the way, I raised this ticket as a precursor to fixes for |
About JIRA... I have tried on two different browsers and two different JIRA accounts, and I can't create tickets. Totally bizarre... it looks like the "Create Ticket" form saves when I press "Submit"--the server even returns 200, but the ticket isn't actually created. |
@p-mongo can you please confirm your previous comment is no longer a concern? |
* master: MONGOID-5029 #empty method should use an #exists? rather than a #count query (mongodb#5029)
Hi @johnnyshields , I added integration tests and I was able to verify the However for the query example I wasn't getting aliases expanded:
Can you confirm whether the |
Yes, (Or |
lib/mongoid/fields.rb
Outdated
"#{segment}.#{relation.klass.database_field_name(remaining)}" | ||
else | ||
"#{segment}.#{remaining}" | ||
end.freeze |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the purpose of the freeze calls in this method please?
This PR as far as I can tell is complete with respect to pluck and distinct, including documentation. Therefore my suggestion would be to merge it as is and tackle For |
Interpolated strings arent frozen by default.
On Fri, Sep 10, 2021 at 13:33 Oleg Pudeyev ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In lib/mongoid/fields.rb
<#5047 (comment)>:
> def database_field_name(name)
- return nil unless name
- normalized = name.to_s
- aliased_fields[normalized] || normalized
+ return nil unless name.present?
+ key = name.to_s
+ segment, remaining = key.split('.', 2)
+ segment = aliased_fields[segment] || segment
+ return segment.freeze unless remaining
+
+ relation = relations[aliased_relations[segment] || segment]
+ if relation
+ "#{segment}.#{relation.klass.database_field_name(remaining)}"
+ else
+ "#{segment}.#{remaining}"
+ end.freeze
What is the purpose of the freeze calls in this method please?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#5047 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAGYB4W2ZBVZN6MUUMAN5LUBGDA3ANCNFSM5CQVIRYQ>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
--
Johnny Shields
Co-Founder & Chief Technology Officer
[image: TableCheck] <https://www.tablecheck.com/en/company/>
[image: Address] Dai 27 Chuo Bldg 4F, 2-14-5 Ginza, Chuo-ku, Tokyo, Japan
104-0061 <https://goo.gl/maps/G4PyDSZE8s32>
[image: Phone] +81 (0)80-3519-5647
[image: Web] ***@***.***
https://www.tablecheck.com/en/company/
|
Thanks for clarifying that. It's a bit hard to tell from the diff but it seems to me that previously the return value of the method was not frozen. Changing it to a frozen string would technically be an API change and we've had an issue in the past with users expecting unfrozen objects from our methods. Is freezing the strings as you originally wrote an optimization only or was it required for functionality? |
Freezing not required. Let's go with your way. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@p-mongo What is the reason of adding commented out tests, Maybe we can use pending
? I executed this file locally, and only test failed.
I removed it and put it in the ticket for future use, if we end up needing it. |
Thanks for the merge, I appreciate it! |
* master: MONGOID-5151 Respect aliased fields in pluck/distinct by having Document.database_field_name recursively consider embedded docs (mongodb#5047) RUBY-2675 test coverage on the Mongoid side (mongodb#5030) MONGOID-4592 Add examples of using read_attribute and write_attribute to implement custom field behavior (mongodb#5082) MONGOId-5185 Remove tests for _id serialization (mongodb#5081) MONGOID-5103 Implement eq symbol operator (mongodb#5076) Fix MONGOID-5006 Link default auth source documentation to driver instead of incorrectly claiming "admin" is always the default (mongodb#5048) Create security policy following github's template (mongodb#5070)
* master: MONGOID-5151 Respect aliased fields in pluck/distinct by having Document.database_field_name recursively consider embedded docs (mongodb#5047) RUBY-2675 test coverage on the Mongoid side (mongodb#5030) MONGOID-4592 Add examples of using read_attribute and write_attribute to implement custom field behavior (mongodb#5082) MONGOId-5185 Remove tests for _id serialization (mongodb#5081) MONGOID-5103 Implement eq symbol operator (mongodb#5076) Fix MONGOID-5006 Link default auth source documentation to driver instead of incorrectly claiming "admin" is always the default (mongodb#5048) Create security policy following github's template (mongodb#5070)
* master: (26 commits) MONGOID-5128 Scoped associations (mongodb#5017) MONGOID-5005 - .sum, .count, and similar aggregables now ignore sort if not limiting/skipping (mongodb#5049) MONGOID-5098 Improve specs for timezone handling (specs only; no behavior change) (mongodb#5023) MONGOID-5151 Respect aliased fields in pluck/distinct by having Document.database_field_name recursively consider embedded docs (mongodb#5047) RUBY-2675 test coverage on the Mongoid side (mongodb#5030) MONGOID-4592 Add examples of using read_attribute and write_attribute to implement custom field behavior (mongodb#5082) MONGOId-5185 Remove tests for _id serialization (mongodb#5081) MONGOID-5103 Implement eq symbol operator (mongodb#5076) Fix MONGOID-5006 Link default auth source documentation to driver instead of incorrectly claiming "admin" is always the default (mongodb#5048) Create security policy following github's template (mongodb#5070) MONGOID-5131 Use proper mdb <-> ubuntu versions (mongodb#5067) MONGOID-5131: Set up build pipeline for outside contributors (mongodb#5043) MONGOID-5165 Fix documentation link (mongodb#5065) Fix indentation MONGOID-5029 #empty method should use an #exists? rather than a #count query (mongodb#5029) MONGOID-5177 Fix flaky contextual spec (mongodb#5064) MONGOID-5170 - Fix more typos mostly in code docs and code comments (mongodb#5056) MONGOID-5105 Allow block form in Mongoid::Association::EmbedsMany::Proxy#count (mongodb#5060) MONGOID-5162 Add a release note for the planned changes in ObjectId#as_json (mongodb#5059) MONGOID-5171 - Make the minimum Ruby version 2.5 (mongodb#5058) ...
* master: MONGOID-5128 Scoped associations (mongodb#5017) MONGOID-5005 - .sum, .count, and similar aggregables now ignore sort if not limiting/skipping (mongodb#5049) MONGOID-5098 Improve specs for timezone handling (specs only; no behavior change) (mongodb#5023) MONGOID-5151 Respect aliased fields in pluck/distinct by having Document.database_field_name recursively consider embedded docs (mongodb#5047) RUBY-2675 test coverage on the Mongoid side (mongodb#5030) MONGOID-4592 Add examples of using read_attribute and write_attribute to implement custom field behavior (mongodb#5082) MONGOId-5185 Remove tests for _id serialization (mongodb#5081) MONGOID-5103 Implement eq symbol operator (mongodb#5076) Fix MONGOID-5006 Link default auth source documentation to driver instead of incorrectly claiming "admin" is always the default (mongodb#5048) Create security policy following github's template (mongodb#5070)
Mongoid::Document.database_field_name
does not currently handle "." delimiter char. As an improvement, it should consider embedded doc:store_as
and embedded field aliases.As a positive side-effect of this PR,
distinct
andpluck
(and maybe other methods) now magically work when using embedded field aliases.This PR also adds
Mongoid::Document.aliased_relations
which is a map analogous toaliased_fields
This is a precursor to further improvements on "distinct" and "pluck" methods.
TODO Tests:
pluck
anddistinct
methods (this PR fixes that)