-
Type: Improvement
-
Resolution: Fixed
-
Priority: Unknown
-
Affects Version/s: None
-
Component/s: None
-
None
-
Fully Compatible
The Enumerable#tally method is incredibly useful for exploratory data analysis:
https://ruby-doc.org/core-2.7.0/Enumerable.html#method-i-tally
["a", "b", "c", "b"].tally #=> {"a"=>1, "b"=>2, "c"=>1}
I would like to implement this on Mongoid::Criteria similar to pluck and distinct:
User.tally(:name) #=> {"Bob"=>3, "Bruce"=>2, "Jane"=>4}
This would use the aggregation pipeline under the hood, and then demongoize the keys. The proposed method interface is a bit different than Ruby's native tally because Ruby tally works on Arrays while this works on BSON docs (i.e. Hash) data, so you need to specify the key which to tally.
(Currently you can do `User.pluck(:name).tally`, however, this isn't efficient for large collections.)
I'm happy to implement this at some point. Please confirm you'd accept the proposed implementation.
- is related to
-
MONGOID-5410 Indifferent access into hashes nested in array field does not work before persistence
- Closed
- related to
-
RUBY-2797 Forward find conditions to View#aggregate
- Closed