3
我有一個用戶和團隊的M-N關係&我想使用一個subdoc策略,而不是標準的3表SQL策略。合併2個基於ID字段的對象subdocs數組
user = {
id: 'user1',
teams: [{'team1', roles: ['admin']}, {'team2', roles: ['editor']}]
}
team = {
id: 'team1',
name: 'The Team #1'
}
我想從team
表搶name
領域,並把它貼在適當的subdoc的:
query = {
id: 'user1',
teams: [{'team1', roles: ['admin'], name: 'The Team #1'}, {'team2', roles: ['editor'], name: 'The Team #2'}]
}
我能得到球隊的文檔很輕鬆了,但我一直覆蓋teams
array:
//Query
r.table('users').get('user1').merge(function(user) {
return {
teams: r.table('teams').getAll(r.args(user('teams')
.map(function(team) {return team('id')}))).coerceTo('array')
}
})
//Bad result
user.teams = [
{
"id": "team1" ,
"name": "team 1"
} ,
{
"id": "team2" ,
"name": "team 2"
}
]
是否有可能根據對象字段合併對象數組,還是應該在應用程序級別執行此操作?有沒有更好的辦法?
這工作很漂亮,謝謝@mlucy!從性能角度來看,它與聯合一樣昂貴,對吧? (對於這個例子顯然不重要,但對於更頻繁的M-N查詢) –
是的,它至少在當前版本的RethinkDB中與聯接一樣昂貴。 – mlucy