How to popular an array of an object based on its Index using Rethinkdb

Asked

Viewed 15 times

1

Hello, I’m starting to "understand" how Rethinkdb works and I’m seeing that it is for sure the Nosql database that comes closest to the logic of an SQL database without losing the optimization and advantages of Nosql. My problem is this, imagine that I have the two structures below that relate students and the materials that 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"] },
  ...
]

So I found in the Rethinkdb documentation, I know I can make a Join for all students in a query when it’s a column for an id. How do I get an array back with all students and their related subjects? I need a feedback as 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’ve even found an example on the internet of how to do this when the reference is between a single key to a "Foreign key" here.

The solution I’m looking for I usually do using Mongodb + Mongoose but I’m willing to test the speed + the "real-time" capability it provides.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.