I have some data that saved in MongoDB,I want to transfer them to MySQL.
I use MongoDB PHP Library to do this,and I write a demo for test below:
MongoDB PHP Library docs:
https://docs.mongodb.com/php-library/master/
http://php.net/manual/en/book.mongodb.php
TestController.php
//insert some test data into 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');
}
//query the data from mongodb and insert them into mysql
public function queryAndInsertMysql()
{
$collection = (new \MongoDB\Client)->test->articles;
$cursor = $collection->find();
//how to write next?
}
In mysql,there is a table articles
,it has these fields:
id
title
content
I want to transfer the query result from mongodb into mysql table articles
.
Question:
In the second function queryAndInsertMysql()
,how to insert the result that queried form mongodb into mysql?