I have a M-N relation of users and teams & I'm trying to use a subdoc strategy instead of a standard 3-table SQL strategy.
user = {
id: 'user1',
teams: [{'team1', roles: ['admin']}, {'team2', roles: ['editor']}]
}
team = {
id: 'team1',
name: 'The Team #1'
}
I'd like to grab the name
field from the team
table and stick it in the appropriate subdoc:
query = {
id: 'user1',
teams: [{'team1', roles: ['admin'], name: 'The Team #1'}, {'team2', roles: ['editor'], name: 'The Team #2'}]
}
I can get the team doc easily enough, but I keep overwriting the 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"
}
]
Is it possible to merge an array of objects based on an object field, or should I do this at the application level? Is there a better way?