0

Say I have a user table with a property called favoriteUsers which is an embedded array. i.e.

users

{
  name:'bob'
  favoriteUsers:['jim', 'tim'] //can you have an index on an embedded array?
}

user_presence

{
   name:'jim', //index on name
   online_since:14440000
}

Can I do an inner or eqJoin against say a 2nd table using the embedded property, or would I have to pull favoriteUsers out of the users table and into a join table like in traditional sql?

r.table('users')
 .getAll('bob', {index:'name'})
  // inner join  user_presence on user_presence.name in users.highlights
 .eqJoin("name", r.table('user_presence'), {index:'name'})

Eventually, I'd like to call changes() on the query so that I can get a realtime update of the users favorite users presence changes

MonkeyBonkey
  • 46,433
  • 78
  • 254
  • 460
  • Can you add some more context to your question to make it clear than a bit? Which table you want to join with which table etc. Because ideally, you can use `eqJoin` with embeded document like the example at very bottom of http://www.rethinkdb.com/api/javascript/eq_join/. You can also use `getAll` and `concatMap` and much more powerful join logic. It may give same effect as `join` despite of the name not related to join. – kureikain Oct 11 '15 at 06:50
  • Sure, I can see that the 2nd code example is a bit unclear. I'm trying to do the join in the code specified above, notice in the other join examples, the join is on a top level non-collection property of a document, e.g. user.name, and not in a document's embedded/nested property user.favoritUsers which is a collection itself and not a property. I'll update the code example to make it clearer. – MonkeyBonkey Oct 11 '15 at 11:44

1 Answers1

1

eqJoin can works on embedded document, but it works by compare a value which we transform/pick from the embedded document to mark secondary index on right table.

In any other complicated join, I would rather use concatMap together with getAll.

Let's say we can fetch user and user_presence of their favoriteUsers

r.table('users')
.getAll('bob', {index: 'name'})
.concatMap(function(user) {
  return r.table('user_presence').filter(function(presence) {
    return user("favoriteUsers").contains(presence("name"))
  })
)

So ideally, now you get the data and do the join yourself by querying extra data that you need. My query may have some syntax/error but I hope it gives you the idea

kureikain
  • 2,304
  • 2
  • 14
  • 9