Rethinkdb 将查询结果插入 table
Rethinkdb insert query results into a table
我正在尝试将一个 table 的查询结果插入另一个 table。但是,当我尝试 运行 查询时,我收到一个错误。
{
"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")
};
});
});
});
);
这是否有特殊的语法,或者我是否必须检索结果然后 运行 单独插入?
问题是您的内部查询正在返回数组流。您不能将数组插入 table(仅限对象),因此查询失败。如果将最外层的 map
更改为 concatMap
,它应该可以工作。
这里的问题是结果是一个对象数组序列。即
[ [ { 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 为我指明了正确的方向。
我正在尝试将一个 table 的查询结果插入另一个 table。但是,当我尝试 运行 查询时,我收到一个错误。
{
"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")
};
});
});
});
);
这是否有特殊的语法,或者我是否必须检索结果然后 运行 单独插入?
问题是您的内部查询正在返回数组流。您不能将数组插入 table(仅限对象),因此查询失败。如果将最外层的 map
更改为 concatMap
,它应该可以工作。
这里的问题是结果是一个对象数组序列。即
[ [ { 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 为我指明了正确的方向。