我有一些数据存在mongodb中,我想把它转移到mysql中,我使用MongoDB PHP Library来连接mongodb数据库,然后写了个测试的demo:
MongoDB PHP Library文档: https://docs.mongodb.com/php-... http://php.net/manual/en/book...
TestController.php
//向mongodb写入一些测试数据,可以成功写入
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查询出来了数据,接下来怎么写入mysql呢?
}
我准备好了一个mysql表articles,它有以下字段:
id
title
content
我想把上面mongodb中的查询出的内容都转移到这个mysql表articles中。
问题: 上面代码第二个函数queryAndInsertMysql()从mongodb查询出了数据,接下来写入mysql应该怎么办呢?
3 回答

- 3 回答
- 0 关注
- 673 浏览
添加回答
举报
0/150
提交
取消