rethinkdb / rethinkdb-go

Go language driver for RethinkDB

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question about EqJoin off of Map or ForEach

mfridman opened this issue · comments

s:help-wanted t:question

Hi,

I have this query that merges; note, Map() is not required but there to showcase train of thought

cur, err := r.DB("db").Table("tbl").Get(i).Merge(func(a r.Term) interface{} {
		return map[string]interface{}{
			"data": r.DB("db").Table("tbl2").Filter(
				map[string]interface{}{"data_id": a.Field("id")}).
				CoerceTo("array").Map(func(m r.Term) interface{} { return m }),
		}
	}).Run(session)

The data TypeOf is data:ARRAY and the result is something like this:

{
    "id": 41298374,
    "data": [{
        "id": 15,
        "data_id": 41298374,
        "endpoint": "bef29b4e-f16b-11e7-8c3f-9a214cf093ae"
    }, {
        "id": 16,
        "data_id": 41298374,
        "endpoint": "bef2a350-f16b-11e7-8c3f-9a214cf093ae"
    }]
}

I then want to iterate over the objects in data and embed (EqJoin/Zip) a matching row from another table to each object that matches endpoint (endpoint is unique).

Presumably I could use Map() or ForEach() off the data array and EqJoin()?

The error I seem to be stuck with is: gorethink: Cannot convert OBJECT to SEQUENCE in

Suggestions would be highly appreciated.

Ah, momentary lapse of judgement. Since I already have an object can just Merge it.

Here is the query that solved this problem for me in case it comes up for someone else, likely room for refactor but it's workable:

I put map[string]interface{}{"KEY": "VALUE"} as an example

cur, err := r.DB("db").Table("tbl").Get(i).Merge(func(a r.Term) interface{} {
	return map[string]interface{}{
		"data": r.DB("db").Table("tbl2").
			Filter(map[string]interface{}{"data_id": a.Field("id")}).
			CoerceTo("array").Filter("data").
			Map(func(c r.Term) interface{} {
				return c.Merge(c, map[string]interface{}{"KEY": "VALUE"})
			}),
	}
}).Run(session)