0

I'm trying to insert the results of a query from one table into another table. However, when I attempt to run the query I am receiving an error.

{
  "deleted": 0 ,
  "errors": 1 ,
  "first_error":  "Expected type OBJECT but found ARRAY." ,
  "inserted": 0 ,
  "replaced": 0 ,
  "skipped": 0 ,
  "unchanged": 0
}

Here is the the insert and query:

r.db('test').table('destination').insert(
  r.db('test').table('source').map(function(doc) {
    var result = doc('result');

    return result('section_list').concatMap(function(section) {
      return section('section_content').map(function(item) {
        return {
          "code": item("code"),
          "name": item("name"),
          "foo": result("foo"),
          "bar": result("bar"),
          "baz": section("baz"),
          "average": item("average"),
          "lowerBound": item("from"),
          "upperBound": item("to")
        };
      });
    });
  });
);

Is there a special syntax for this, or do I have to retrieve the results and then run a separate insert?

scull7
  • 105
  • 1
  • 5

2 Answers2

0

The problem is that your inner query is returning a stream of arrays. You can't insert arrays into a table (only objects), so the query fails. If you change the outermost map into a concatMap it should work.

mlucy
  • 5,249
  • 1
  • 17
  • 21
0

The problem here was that the result was a sequence of an array of objects. i.e

[ [ { a:1, b:2 }, { a:1, b:2 } ], [ { a:2, b:3 } ] ]

Therefore, I had to change the outer map call to a concatMap call. The query then becomes:

r.db('test').table('destination').insert(
  r.db('test').table('source').concatMap(function(doc) {
    var result = doc('result');

    return result('section_list').concatMap(function(section) {
      return section('section_content').map(function(item) {
        return {
          "code": item("code"),
          "name": item("name"),
          "foo": result("foo"),
          "bar": result("bar"),
          "baz": section("baz"),
          "average": item("average"),
          "lowerBound": item("from"),
          "upperBound": item("to")
        };
      )});
    });
  });
}

Thanks goes to @AtnNn on the #rethinkdb freenode for pointing me in the right direction.

scull7
  • 105
  • 1
  • 5