我有一些數(shù)據(jù)存在mongodb中,我想把它轉(zhuǎn)移到mysql中,我使用MongoDB PHP Library來連接mongodb數(shù)據(jù)庫,然后寫了個測試的demo:
MongoDB PHP Library文檔: https://docs.mongodb.com/php-... http://php.net/manual/en/book...
TestController.php
//向mongodb寫入一些測試數(shù)據(jù),可以成功寫入
public function insertMongodb()
{
$collection = (new \MongoDB\Client)->test->articles;
$collection->insertMany([
['title' => 'hello', 'content' => 'hello...'],
['title' => 'foo', 'content' => 'foo...'],
['title' => 'bar', 'content' => 'bar...'],
]);
dd('ok');
}
//從mongodb查詢出來,寫入mysql
public function queryAndInsertMysql()
{
$collection = (new \MongoDB\Client)->test->articles;
$cursor = $collection->find();
//上面從mongodb查詢出來了數(shù)據(jù),接下來怎么寫入mysql呢?
}
我準備好了一個mysql表articles,它有以下字段:
id
title
content
我想把上面mongodb中的查詢出的內(nèi)容都轉(zhuǎn)移到這個mysql表articles中。
問題: 上面代碼第二個函數(shù)queryAndInsertMysql()從mongodb查詢出了數(shù)據(jù),接下來寫入mysql應(yīng)該怎么辦呢?
3 回答

慕田峪7331174
TA貢獻1828條經(jīng)驗 獲得超13個贊
用這個擴展包,根據(jù)文檔配置一下,然后直接使用同一個Model就行了,操作前手動指定一下connection 的dirver
- 3 回答
- 0 關(guān)注
- 685 瀏覽
添加回答
舉報
0/150
提交
取消