laravel怎么把从mongoDB查询出来的内容写入MySQL?

我有一些数据存在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应该怎么办呢?

潇湘沐
浏览 674回答 3
3回答

潇潇雨雨

-最直接的方法是使用php的mysql模块建立连接,拼SQL语句写入数据库,但是几乎每一个表都要重新写一次。

慕田峪7331174

Laravel Mongodb 用这个扩展包,根据文档配置一下,然后直接使用同一个Model就行了,操作前手动指定一下connection 的dirver
打开App,查看更多内容
随时随地看视频慕课网APP