Jeremy Smith
Jeremy Smith

Reputation: 15079

Is it possible to only retrieve unique values from a document in mongo?

I'm guessing the answer is no, but is this possible? It seems like this is too much code for something so simple:

ary = []
obj.all.each {|o| ary << o[:foo]}
ary.uniq!

Upvotes: 1

Views: 996

Answers (1)

Phrogz
Phrogz

Reputation: 303431

Separate from Mongo, you can write that same functionality in Ruby better as:

ary = obj.all.map{ |o| o[:foo] }.uniq

Edit: It looks like Mongo supports this via distinct:

ary = @db['pageviews'].distinct('ip-address')

See the documentation for more details.

Upvotes: 3

Related Questions