user3630910
user3630910

Reputation: 11

How do I get unique field values using rethinkdb javascript?

I have a field which has similar values. For eg {country : 'US'} occurs multiple times in the table. Similar for other countries too. I want to return an array which contains non-redundant values of 'country' field. I am new to creating Databases so likely this is a trivial question but I couldn't find anything useful in rethinkdb api.[SOLVED]

Thanks

Upvotes: 1

Views: 1173

Answers (1)

neumino
neumino

Reputation: 4353

You can use distinct, but the distinct command was created for short sequences only. If you have a lot of data, you can use map/reduce

r.table("data").map(function(doc) {
    return r.object(doc("country"), true) // return { <country>: true}
}).reduce(function(left, right) {
    return left.merge(right)
}).keys() // return all the keys of the final document

Upvotes: 3

Related Questions