How to populate an array of an object based on its indexes using RethinkDB

1
Hello, I'm starting to "understand" how RethinkDB works and I'm seeing that it's certainly the NoSQL database that comes closest to the logic of a SQL database without losing the optimization and benefits of NoSQL. My problem is this, imagine that I have the 2 structures below that relate students and the subjects they study:

// table matérias
"materias" : [
  { id: "m-1", nome: "Português" },
  { id: "m-2", nome: "Matemática" },
  { id: "m-3", nome: "História" },
  ...
]
// table alunos
"alunos" : [
  {id: "a-1", nome: "João", materias: ["m-1", "m-2"] },
  {id: "a-2", nome: "Mari", materias: ["m-1"] },
  {id: "a-3", nome: "José", materias: ["m-1", "m-2", "m-3"] },
  ...
]

As I found in the RethinkDB documentation , I know I can make a join for all students in a query when it is a column for an id. How should I do so that I can return an array with all students and their related subjects? I need a return like the example below:

"alunos": [
  {
    id: "a-1", 
    nome: "João", 
    materias: [
      { id: "m-1", nome: "Português" },
      { id: "m-2", nome: "Matemática" }
    ] 
  },
  {
    id: "a-2", 
    nome: "Mari", 
    materias: [
      { id: "m-1", nome: "Português" }
    ] 
  },
  {
    id: "a-1", 
    nome: "João", 
    materias: [
      { id: "m-1", nome: "Português" },
      { id: "m-2", nome: "Matemática" },
      { id: "m-3", nome: "História" }
    ] 
  }
]

I even found an example on the internet how to do this when the reference is between a unique key to a "foreign key" here .

  

The solution I'm looking for is already done using MongoDB + Mongoose but I'm trying to test the speed + the "real-time" capability it provides.

    
asked by anonymous 10.11.2017 / 13:06

0 answers