Reputation: 467
Lets say I have a "products" table in rethinkdb, and each product can have lots of tags. I want to retrieve all distinct tags I have in my system (without adding an additional "tags" table) Currently I'm doing this (with an addition of auto-complete filtering):
r.table('products')
.getAll('[email protected]', { index: "email" })
.filter(function (doc) {
return doc.hasFields(['tags']).and(doc('tags').contains(function(tag) {
return tag.upcase().match("^a".toUpperCase());
}));
})
.pluck('tags')
.concatMap(function (row) {
return row('tags').filter(function(tag) {
return tag.upcase().match("^a".toUpperCase());
})
})
.distinct()
It seems that I'm not using the fact that I have a good index 'email_tags' that already probably has this information, I want to do something like this:
r.table('products_email_tags_index_table')
.between(['[email protected]', 'A'],
['[email protected]', 'B'],
{index: 'email_tags', left_bound: 'open', right_bound: 'closed'})
.map(function(indexField) { return indexField.get(1) })
(even though it doesn't keep the casings...)
So my question is: is there any way of querying a secondary index directly?
Upvotes: 1
Views: 266
Reputation: 5289
distinct
can accept an index. Does this do what you want?
r.table('products_email_tags_index_table')
.between(['[email protected]', 'A'],
['[email protected]', 'B'],
{index: 'email_tags', left_bound: 'open', right_bound: 'closed'})
.distinct({index: 'email_tags'})
Upvotes: 2