rethinkdbrethinkdb-javascriptreql

RethinkDb Join array in object with objects


I'm using rethinkdb with JS.

I have a rethinkdb table 'users'. One user object looks like this:

{
  id: '1',
  friends: [
    '2',
    '4'
  ],
  items: [
    {
      id: '1',
      name: 'test item 1'
    }
  ]
}

A user is allowed to see all items of himself and of his friends. Now I want to get all items a user is allowed to see.

But I don't now how to do that. With a join or with a merge? Or is my data structure wrong?

Best regards.


Solution

  • You can get all friends with getAll and then map items:

    r.db("YourDataBase").table("YoorTable")
        .getAll(r.args(r.db("YourDataBase").table("YoorTable").get("a1")("friends")))
        .map(function(val){
          return val("items");
        })