我正在使用laravel-mongodb
(https://github.com/jenssegers/laravel-mongodb)來操作mongodb數據。如何從mongdoDB的查詢結果中刪除`_id`字段?
我想將數據傳輸到MySQL,從MongoDB的查詢,還有一個演示功能,要做到這一點:
TestController.php
public function queryAndInsertMysql()
{
$articles = DB::connection('mongodb')->collection('articles')->get()->toArray();
DB::connection('mysql')->table('articles')->insert($articles);
dd('ok');
}
有這個函數運行時錯誤:
SQLSTATE[42S22]: Column not found: 1054 Unknown column '_id' in 'field list' (SQL: insert into `articles` (`_id`, `content`, `title`) values (59ad186437dd1f1968004334, hello..., hello), (59ad186437dd1f1968004335, foo..., foo), (59ad186437dd1f1968004336, bar..., bar))
出現該錯誤是因爲在mongodb集合中有_id
字段,
並且有i s沒有_id
字段在mysql表中,
所以我需要刪除_id
字段,然後再將它們插入到mysql中。
我嘗試用array_shift()
做到這一點,但它不工作:
public function removeId()
{
$articles = DB::connection('mongodb')->collection('articles')->get()->toArray();
foreach ($articles as $article) {
array_shift($article);
}
dd($articles);
}
的_id
領域仍然存在:
我該怎麼辦?