【问题标题】:Rethinkdb insert query results into a tableRethinkdb 将查询结果插入表中
【发布时间】:2015-09-18 20:26:35
【问题描述】:

我正在尝试将查询结果从一个表插入到另一个表中。但是,当我尝试运行查询时收到错误消息。

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

这里是插入和查询:

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")
        };
      });
    });
  });
);

是否有特殊的语法,或者我必须检索结果然后运行单独的插入?

【问题讨论】:

    标签: rethinkdb rethinkdb-javascript


    【解决方案1】:

    问题是您的内部查询正在返回一个数组流。您不能将数组插入表(仅限对象),因此查询失败。如果您将最外层的map 更改为concatMap,它应该可以工作。

    【讨论】:

      【解决方案2】:

      这里的问题是结果是一个对象数组的序列。即

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

      因此,我不得不将外部 map 调用更改为 concatMap 调用。然后查询变为:

      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")
              };
            )});
          });
        });
      }
      

      感谢#rethinkdb freenode 上的@AtnNn 为我指明了正确的方向。

      【讨论】:

        猜你喜欢
        • 2014-10-20
        • 1970-01-01
        • 2015-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-08
        • 2013-12-25
        • 2016-09-09
        相关资源
        最近更新 更多