anli
anli

Reputation: 735

How can I filter if any value of an array is contained in another array in rethinkdb/reql?

I want to find any user who is member of a group I can manage (using the webinterface/javascript):

Users:

{
  "id": 1
  "member_in_groups": ["all", "de-south"]
},
{
  "id": 2
  "member_in_groups": ["all", "de-north"]
}

I tried:

r.db('mydb').table('users').filter(r.row('member_in_groups').map(function(p) {
  return r.expr(['de-south']).contains(p);
}))

but always both users are returned. Which command do I have to use and how can I use an index for this (I read about multi-indexes in https://rethinkdb.com/docs/secondary-indexes/python/#multi-indexes but there only one value is searched for)?

Upvotes: 0

Views: 419

Answers (1)

anli
anli

Reputation: 735

I got the correct answer at the slack channel so posting it here if anyone else comes to this thread through googling:

First create a multi index as described in https://rethinkdb.com/docs/secondary-indexes/javascript/, e. g.

r.db('<db-name>').table('<table-name>').indexCreate('<some-index-name>', {multi: true}).run()

(you can omit .run() if using the webadmin)

Then query the data with

r.db('<db-name>').table('<table-name>').getAll('de-north', 'de-west', {index:'<some-index-name>'}).distinct()

Upvotes: 0

Related Questions