I have two tables, users
and authors
.
A user can be an author and if so, in the author document there is the id
of the user.
What I want to query are all the users that are NOT authors. The equivalent mySQL query would be something like
select * from users left join authors on users.id=authors.userId
where authors.userId is null
What I tried is
r.db('journa').table('authors').outerJoin(
r.db('journa').table('users'),
(author, user) => {
return author.hasFields({'left': 'claimedByUserId'}).not()
}
)
But it's not working. Any idea on how to achieve it?
You were almost on the right way. You should not invert in the join expression, because it will return a set of all non-matching permutations and you won't be able to detect a required match. The following query seems to accomplish what you need:
r.db('journa')
.table('users')
.outerJoin(
r.db('journa').table('authors'),
// This is what two sequences are joined upon
(user, author) => user('id').eq(author('userId'))
)
// Joins return `left` and `right`.
// If there's nothing at the right side, we assume there was no match
.filter((lr) => lr.hasFields('right').not())
// Extracting the left table record only
.map((lr) => lr('left'))