Skip to content

Commit 21feecd

Browse files
authored
MONGOID-5270 Implement .tally method for Mongoid#Criteria (#5344)
* MONGOID-5270 initial implementation of tally * MONGOID-5270 finish and fully test tally feature * MONGOID-5270 add typeless field test * MONGOID-5270 don't change existing behavior * MONGOID-5270 correct documentation * MONGOID-5702 docs, release note, renaming * MONGOID-5270 add none case * MONGOID-5270 tweaks to pluck and tally logic to make them function correctly and consistently * MONGOID-5270 update comment * MONGOID-5270 add tally implementation * MONGOID-5270 change to not use tally * MONGOID-5270 add duplicate keys test * MONGOID-5270 move fetch_and_demongoize and change indifferent access * MONGOID-5270 use eq instead of be * Revert "MONGOID-5270 use eq instead of be" This reverts commit 25d65effdb711fd61737a35610106ddc2723671c.
1 parent 54c30e1 commit 21feecd

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

source/reference/queries.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,27 @@ Mongoid also has some helpful methods on criteria.
13801380
# expands out to "managers.name" in the query:
13811381
Band.all.pluck('managers.n')
13821382

1383+
* - ``Criteria#tally``
1384+
1385+
*Get a mapping of values to counts for the provided field.*
1386+
1387+
*This method accepts the dot notation, thus permitting referencing
1388+
fields in embedded associations.*
1389+
1390+
*This method respects :ref:`field aliases <field-aliases>`,
1391+
including those defined in embedded documents.*
1392+
1393+
-
1394+
.. code-block:: ruby
1395+
1396+
Band.all.tally(:name)
1397+
1398+
Band.all.tally('cities.name')
1399+
1400+
# Using the earlier definition of Manager,
1401+
# expands out to "managers.name" in the query:
1402+
Band.all.tally('managers.n')
1403+
13831404

13841405
Eager Loading
13851406
=============

source/release-notes/mongoid-8.0.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ it was stored and returned as a String.
527527
# => Mongoid 7: "data!"
528528
# => Mongoid 8: <BSON::Binary:0x2600 type=generic data=0x6461746121...>
529529

530+
530531
``Decimal128``-backed ``BigDecimal`` fields
531532
-------------------------------------------
532533

@@ -540,3 +541,35 @@ feature flag turned off, values assigned to fields of type ``BigDecimal`` are
540541
stored as Strings. See the section on :ref:`BigDecimal Fields <bigdecimal-fields>`
541542
for more details.
542543

544+
545+
Implemented ``.tally`` method on ``Mongoid#Criteria``
546+
-----------------------------------------------------
547+
548+
Mongoid 8 implements the ``.tally`` method on ``Mongoid#Criteria``. ``tally``
549+
takes a field name as a parameter and returns a mapping from values to their
550+
counts. For example, take the following model:
551+
552+
.. code::
553+
554+
class User
555+
include Mongoid::Document
556+
field :age
557+
end
558+
559+
and the following documents in the database:
560+
561+
.. code::
562+
563+
{ _id: 1, age: 21 }
564+
{ _id: 2, age: 21 }
565+
{ _id: 3, age: 22 }
566+
567+
Calling ``tally`` on the age field yields the following:
568+
569+
.. code::
570+
571+
User.tally("age")
572+
# => { 21 => 2, 22 => 1 }
573+
574+
The ``tally`` method accepts the dot notation and field aliases. It also
575+
allows for tallying localized fields.

0 commit comments

Comments
 (0)