2015-09-18 87 views
0

我試圖從一個表插入查詢結果到另一個表。但是,當我嘗試運行查詢時,我收到一個錯誤。Rethinkdb將查詢結果插入表

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

是否有這種特殊的語法,或者我需要檢索結果,然後運行一個單獨的插入?

回答

0

問題是您的內部查詢正在返回數組流。您不能將數組插入表(僅限對象),因此查詢失敗。如果將最外層的map更改爲concatMap,它應該可以工作。

+0

謝謝你的作品現在出色。 – scull7

0

這裏的問題是結果是一個對象數組的序列。即

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

感謝@AtnNn關於#rethinkdb freenode指向我在正確的方向。