Bene Bene - 5 months ago 60
SQL Question

gorethink inner join

I don't quit understand joins at all and unfortunately there is very few documentation about it in german. And in my special case i also don't really understand the driver.

So i have a table 'categories' and there i have two docs:

{"id":1, "title":"Category 1"}
{"id":2, "title":"Category 2"}

Than i have a second table 'forums' and there i have three docs:

{"id":1, "title":"Forum 1", "categoryId":1}
{"id":2, "title":"Forum 2", "categoryId":1}
{"id":3, "title":"Forum 3", "categoryId":2}

What i want as an result is:

[{"id":1, "title":"Category 1", "forums":[{"id":1, "title":"Forum 1"},{"id":2, "title":"Forum 2"}]}, {"id":2, "title":"Category 2", "forums":[{"id":3, "title":"Forum 3"}]}]

I know that RethinkDB hasn't a to big community especially in Go, therefore it would also help me if you tell how the SQL command for that would be.

And if there is a Go developer who is in common with the RethinkDB driver maybe you can help be find a solution with Go. Because what i also don't understand is how i can translate this JavaScript code ( to Go, since the arguments (marvelRow, dcRow) in the function need declarations in Go, but i have no idea which.

I am very glad for very hint!


Let's first write it in JavaScript, then we will do it in Go, so you know how to do it next time.

What you want can easily do with either join or mapping. However, join will return a paird of match document, not in the nested way as you want. We can however map it like this:

  .merge(function(cat) {
    return {
      forums: r.table('forums').filter({categoryId: cat('id')}).without('categoryId').coerceTo('array')

Now let's move to Go lang.

package main

import (
    r ""

func main() {

    s, _ := r.Connect(r.ConnectOpts{
        Address:  "",
        Database: "test",
        MaxIdle:  10,
        MaxOpen:  10,

    res, err := r.DB("test").Table("categories").Merge(func(doc r.Term) interface{} {
        return map[string]interface{}{
            "forums": r.DB("test").Table("forums").Filter(map[string]interface{}{
                "categoryId": doc.Field("id"),


    var row interface{}
    for res.Next(&row) {

Everything is almost identical, except that in Go lang, you have to specified type. So you convert JS anonoymous function into Go lang anonymous function, but now everything will have type of r.Term most of time. You also have to specify return type, as we use interface in our case. JavaScript object becomes map[string]interface{} now.

In general, just go to this page and started to convert the JS one step at a time. It's almost one-one mapping except the type.