Skip to content

Commit 4d819bb

Browse files
johnnyshieldspcomandeo-mongo
authored
MONGOID-5151 Respect aliased fields in pluck/distinct by having Document.database_field_name recursively consider embedded docs (#5047)
* MONGOID-???? database_field_name should work with embedded fields 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. * add integration tests * add documentation * add release note * relations -> associations * mark private * replace freezes with dup * Comment out the where test * relations -> associations * remove commented out test Co-authored-by: shields <[email protected]> Co-authored-by: Oleg Pudeyev <[email protected]> Co-authored-by: Dmitry Rybakov <[email protected]>
1 parent 0efdc2f commit 4d819bb

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

source/reference/queries.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,12 @@ Mongoid also has some helpful methods on criteria.
13241324

13251325
*Get a list of distinct values for a single field. Note this will always hit
13261326
the database for the distinct values.*
1327+
1328+
*This method accepts the dot notation, thus permitting referencing
1329+
fields in embedded associations.*
1330+
1331+
*This method respects :ref:`field aliases <field-aliases>`,
1332+
including those defined in embedded documents.*
13271333

13281334
-
13291335
.. code-block:: ruby
@@ -1332,6 +1338,18 @@ Mongoid also has some helpful methods on criteria.
13321338
Band.where(:fans.gt => 100000).
13331339
distinct(:name)
13341340

1341+
Band.distinct('cities.name')
1342+
1343+
# Assuming an aliased field:
1344+
class Manager
1345+
include Mongoid::Document
1346+
embedded_in :band
1347+
field :name, as: :n
1348+
end
1349+
1350+
# Expands out to "managers.name" in the query:
1351+
Band.distinct('managers.n')
1352+
13351353
* - ``Criteria#each``
13361354

13371355
*Iterate over all matching documents in the criteria.*
@@ -1477,12 +1495,24 @@ Mongoid also has some helpful methods on criteria.
14771495

14781496
*Get all the values for the provided field.
14791497
Returns nil for unset fields and for non-existent fields.*
1498+
1499+
*This method accepts the dot notation, thus permitting referencing
1500+
fields in embedded associations.*
1501+
1502+
*This method respects :ref:`field aliases <field-aliases>`,
1503+
including those defined in embedded documents.*
14801504

14811505
-
14821506
.. code-block:: ruby
14831507

14841508
Band.all.pluck(:name)
14851509

1510+
Band.all.pluck('cities.name')
1511+
1512+
# Using the earlier definition of Manager,
1513+
# expands out to "managers.name" in the query:
1514+
Band.all.pluck('managers.n')
1515+
14861516

14871517
Eager Loading
14881518
=============

source/release-notes/mongoid-7.4.txt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,43 @@ which is `under consideration <https://jira.mongodb.org/browse/MONGOID-5158>`_,
111111
Mongoid 7.4 and later will inherit the new implementation provided by
112112
``bson-ruby`` while Mongoid 7.3 and earlier will continue with the
113113
implementation returning a hash of ``{"$oid" => "..."}``.
114+
115+
116+
``distinct`` and ``pluck`` Respect Field Aliases In Embedded Documents
117+
----------------------------------------------------------------------
118+
119+
When ``distinct`` and ``pluck`` are used with aliased fields in embedded
120+
documents, the aliases are now expanded. Given the following definitions:
121+
122+
.. code-block:: ruby
123+
124+
class Band
125+
include Mongoid::Document
126+
embeds_many :managers
127+
end
128+
129+
class Manager
130+
include Mongoid::Document
131+
embedded_in :band
132+
133+
field :name, as: :n
134+
end
135+
136+
Mongoid 7.4 behavior:
137+
138+
.. code-block:: ruby
139+
140+
# Expands out to "managers.name" in the query:
141+
Band.distinct('managers.n')
142+
Band.pluck('managers.n')
143+
144+
Mongoid 7.3 behavior:
145+
146+
.. code-block:: ruby
147+
148+
# Sends "managers.n" without expanding the alias:
149+
Band.distinct('managers.n')
150+
Band.pluck('managers.n')
151+
152+
Note that the alias expansion for top-level fields has already been
153+
done by Mongoid 7.3.

0 commit comments

Comments
 (0)