This article mainly introduces you to the relevant information about node.js synchronizing MongoDB data to MySQL. The article introduces it in great detail through sample code. It has certain reference learning value for everyone's study or work. Friends who need it Let’s study together below.
Preface
Recently due to business needs, the APP backend needs to synchronize the data in MongoDB to the MySQL in the Java backend, and then Synchronize the calculated data in MySQL to the MongoDB database.
This process seems very cumbersome, but in fact it is a process of writing tables to each other.
Next, let’s take a look at the implementation process of node.js inserting data from MongoDB into the MySQL database in batches. Not much to say, let’s take a look at the detailed introduction.
Environment
node.js
MongoDB
MySQL
npm
##Required modules
Prepare data in MongoDB
I will not introduce the Schema of mongoose here I have written it, you can check it online. The node.js connection to MongoDB and MySQL pool is as follows: node.js connection to MongoDB:
//www.jb51.net/article/98813 .htmNodejs mysql pool usage example:
mysql module is felixge/node-mysqlThe source code is as follows:
/** * Created by kevalin on 2015/4/22. */ var express = require('express'); var router = express.Router(); var mysql = require('mysql'); var conf = require('../config/dbconnection'); //定义pool池 var pool = mysql.createPool( { host : conf.dbMysql.host, user : conf.dbMysql.user, password : conf.dbMysql.password, database : conf.dbMysql.database, port : conf.dbMysql.port } ); router.get('/', function(req, res) { var selectSites = "select *, date_format(do_time, '%Y-%m-%d %H:%i:%s') as time from siteinfo order by id"; pool.getConnection(function(err, connection) { if (err) throw err; connection.query(selectSites, function(err, rows) { if (err) throw err; res.render('sites', {title : '站点分布', results : rows}); //回收pool connection.release(); }); }); }); module.exports = router;
Idea:
First query the data from MongoDB and then insert it into MySQL through traversal.User.find({}, (err, user) => { if (err) res.send(err); for( let i = 0 ; i < family.length ; i ++ ) { console.log("第" + (i + 1) + "条数据"); let username = user[i].username; let email = user[i].email; let password = user[i].password; let sql = "insert into user_table(username, email, password) values ('" + username + "','" + email + "','" + password + "');"; pool.query(sql,(err, rows) => { if (err) res.send(err); res.json({ message:'数据插入成功', rows }); }); } });
How to implement breakpoint debugging ts files in Angular2
How to implement lazy loading of routes in vue-router
How to make a cone using JS canvas
Summary of JS sorting algorithm
The above is the detailed content of How to synchronize MongoDB data to MySQL in node.js. For more information, please follow other related articles on the PHP Chinese website!