Combine array of documents in RethinkDB

Let's say I have a table called bookshelf

. I am creating a bookshelf document that has an array of IDs that refers to books in another table called books. I'm trying to use getAll

and merge

to combine documents into a query, but I can't seem to pass all the elements to getAll

. I believe that getAll interprets the ID array as a single literal "object".

Document in the bookshelf:

{
    id: 'MyShelf'
    books: [ 1, 2, 6, 9 ]
}

      

Book document:

{
    id: 1
    name: 'Merging in RethinkDB for Dummies'
}

      

My request:

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: [ ]
}

      

+3


source to share


1 answer


It turns out I needed to use r.args

to turn the array into actual arguments for use with the function getAll

.

Here's 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"
}

      

+5


source







All Articles