Reputation: 45941
Consider a collection of Users:
{ name: 'Jeff' }
{ name: 'Joel' }
Is there a way to efficiently get all the unique values for name
?
User.pluck(:name).uniq
To return
[ 'Jeff', 'Joel' ]
I think this would get the whole collection, so it would be inefficient.
However, if there is an index on name
, is there a way to get all the unique values without getting all the documents?
Or is there another way to efficiently get the unique names
?
Upvotes: 0
Views: 1933
Reputation: 312095
As indicated in the comments, you can efficiently get the unique values of a field over all docs in a collection using distinct
.
The documentation specifically mentions that indexes are used when possible, and that they can cover the distinct query. This means that only the supporting index needs to be loaded into memory to get the results.
When possible,
db.collection.distinct()
operations can use indexes.Indexes can also cover
db.collection.distinct()
operations. See Covered Query for more information on queries covered by indexes.
In Ruby, you would perform your distinct query as:
User.distinct(:name)
Upvotes: 2