Let's say I have a table called bookshelf
. I create a bookshelf document that has an array of IDs that references books in another table called books. I am trying to use getAll
and merge
to combine the documents in a query but I cannot seem to be able to pass all the elements into getAll
. I believe getAll is interpreting the array of IDs as a single literal "object".
A bookshelf document:
{
id: 'MyShelf'
books: [ 1, 2, 6, 9 ]
}
A book document:
{
id: 1
name: 'Merging in RethinkDB for Dummies'
}
My query:
r.db('test').table('bookshelf').merge(function(bookshelf) {
return {
booksOnShelf: r.table('books').getAll(bookshelf('books')).coerceTo('array')
}
})
Expected result:
{
id: 'MyShelf'
books: [ 1, 2, 6, 9 ],
booksOnShelf: [
{ id: 1, name: 'Merging in RethinkDB for Dummies' },
{ id: 2, name: 'Cool Title!' },
...
]
}
Actual result:
{
id: 'MyShelf'
books: [ 1, 2, 6, 9 ],
booksOnShelf: [ ]
}
It turns out that I needed to use r.args
to turn the array into actual arguments for use with the getAll
function.
Here is the correct query:
r.db('test').table('bookshelf').merge(function(bookshelf) {
return {
booksOnShelf: r.table('books').getAll(r.args(bookshelf('books'))).coerceTo('array')
}
})
And the result:
{
"books": [ 1, 2 ],
"booksOnShelf": [{
"id": 1,
"title": "RethinkDB for Dummies"
}, {
"id": 2,
"title": "Awesome Title!"
}],
"id": "MyShelf"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With