r.table('users').hasFields('name').concatMap(function(user) {
return r.table("bets").getAll(
user('userID'),
{ index: "userID" }
).map(function(bet) {
return { left: user, right: bet }
})
})
Goal: Read all users and their bets. All bets should be in an array.
{
"left": {
"name": "Eki95",
"userID": "525dc15b-ebde-464b-a936-bab4ed7e4965"
},
"right": [{
"id": "4987c183-edc1-4cd0-9c8d-f51357b86908",
"bet": "foo",
"userID": "525dc15b-ebde-464b-a936-bab4ed7e4965",
},
{
"id": "18e95569-107f-4182-85c1-7df269880978",
"bet": "bar",
"userID": "525dc15b-ebde-464b-a936-bab4ed7e4965",
},
]
}
What I actually get:
{
"left": {
"name": "Eki95",
"userID": "525dc15b-ebde-464b-a936-bab4ed7e4965"
},
"right": {
"bet": "foo",
"id": "4987c183-edc1-4cd0-9c8d-f51357b86908",
"userID": "525dc15b-ebde-464b-a936-bab4ed7e4965",
}
},
{
"left": {
"name": "Eki95",
"userID": "525dc15b-ebde-464b-a936-bab4ed7e4965"
},
"right": {
"bet": "bar",
"id": "18e95569-107f-4182-85c1-7df269880978",
"userID": "525dc15b-ebde-464b-a936-bab4ed7e4965",
}
},
I could get my result by looping all entries, which is kind of a pain. Is there any way to do this with a query in rethinkdb?
Thanks to https://stackoverflow.com/users/3200554/mlucy, I could get it working:
r.table('users').hasFields('name').map(function(user) {
return {
user: user,
bets: r.table('bets').getAll(user('userID'), {index: 'userID'}).coerceTo('array'),
};
}
I had to adjust the query a bit and add coerceTo('array')
because otherwise it would have thrown a
ReqlQueryLogicError: Expected type DATUM but found SELECTION
exception.