Search code examples
rethinkdbrethinkdb-javascript

Rethinkdb insert query results into a table


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?


Solution

  • 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.