Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-5270

Implement .tally method for Mongoid#Criteria

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 8.0.1
    • 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.

            Assignee:
            neil.shweky@mongodb.com Neil Shweky (Inactive)
            Reporter:
            shields@tablecheck.com Johnny Shields
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: