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

Getting a distinct count of a field

    • Type: Icon: Task Task
    • Resolution: Done
    • 4.0.0 final
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      Sometimes we want to get a distinct count of fields, but not the distinct values themselves. So we do Comment.distinct(:author_id).size. This works, but requires loading the whole array of author_ids into memory first which doesn't scale quite as well.

      I was curious if there was a convenient way to do this all in the db.

      It seems doable with a custom aggregation or map reduce, where you do the .size in the reduce step. or pipeline. But I was just curious if Mongoid had an easier built in way to do this. Thanks!

            Assignee:
            Unassigned Unassigned
            Reporter:
            barmstrong Brian Armstrong
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: