RethinkDb: Has many query

40 views Asked by At

Have two table, Quiz and Questions

Quiz table store questions id and its display position. Display position is different for every Quiz.

Quiz table:

{
 id: '1'
 name: 'Quiz 1'
 questions: [
  {
   question_id: '1',
   position: 4
  },
  {
   question_id: '2',
   position: 1
  },
  ......
 ]
}
...

Question Table:

[
 {
  id: '1',
  title: 'Question 1'
 },
 {
  id: '2'
  title: 'Question 2'
 }
]

I want the the return result like this

{
 id: '1',
 name: 'Quiz 1',
 questions: [
   {
    position: 4,
    title: 'Question 1'
   },
   {
    position: 1,
    title: 'Question 2'
   }
 ]
}

How can i get the desired result?

thanks

1

There are 1 answers

0
Jawad On

I got the desired result by using following query

r.db('database').table("quizes").get(id)
  .do((quiz) => {
    return quiz.merge({'questions': quiz('questions').map((q) => {
     return q.merge((r.db('database').table('questions').get(q('id')).without('id')))
    })
   })
 })