2015-12-16 50 views
0

我在加入兩個表時遇到了一些問題。使用RethinkDB加入陣列

我有兩個表:

QAS

[{ 
    "course": "swd" , 
    "id": "c9b2e8cb-15f9-4f93-b677-6dff2880d383" , 
    "number": 1 , 
    "questions": [ 
    { 
     "date": "Wednesday, December 16th, 2015, 11:09" , 
     "owner": 4362445 , 
     "question": "Could you explain promises?" , 
     "questionid": "2766d4bf-fd79-4f94-8d22-788d4e6b89c2" , 
    } 
] , 
"session": 1 , 
"unit": 1 
}] 

用戶

[{ 
    "avatar_url": https://avatars.githubusercontent.com/u/4362445?v=3, » 
    "displayName": "Jamie" , 
    "id": 4362445 , 
    "oauth_token": "46451371bffea867a71b9cc357eff4fd9a06591e" , 
    "role": "student" , 
    "team": { 
    "id": 1729535 , 
    "name": "dwa-group-a" 
} , 
"username": "Jamiek94" 
}] 

現在我想加盟(QAS - >問題 - >所有者) (用戶 - > ID),最後將它們壓縮到最大。 所以結果是這樣的:

[{ 
    "course": "swd" , 
    "id": "c9b2e8cb-15f9-4f93-b677-6dff2880d383" , 
    "number": 1 , 
    "questions": [ 
    { 
     "date": "Wednesday, December 16th, 2015, 11:09" , 
     "owner": 4362445 , 
     "question": "Could you explain promises?" , 
     "questionid": "2766d4bf-fd79-4f94-8d22-788d4e6b89c2" , 
     "user" : { 
     "avatar_url": https://avatars.githubusercontent.com/u/4362445?v=3, » 
     "displayName": "Jamie" , 
     "id": 4362445 , 
     "oauth_token": "46451371bffea867a71b9cc357eff4fd9a06591e" , 
     "role": "student" , 
     "team": { 
     "id": 1729535 , 
     "name": "dwa-group-a" 
    } 
    } 
] , 
"session": 1 , 
"unit": 1 
}] 

我使用的查詢是:

r.db('GitSmurf').table('qa').eqJoin(r.row('questions')('owner'), r.db('GitSmurf').table('users'), { index : 'id'}) 

這導致:

沒有結果此查詢

返回
+0

重新思考有一個'union'命令可以幫助:https://rethinkdb.com/api/javascript/union/ – nha

回答

1

你的questions是一個數組,所以你canno t使用r.row('questions')('owner')

我們可以從eqJoinmap手動加入。像這樣的東西應該適合你:

r.table('Qas').merge(function(qa) { 
    return { 
    questions: qa('questions').map(function(q) { 
     return q.merge({user: r.table('Users').get(q('owner'))}) 
    }) 
    } 
})