Home  >  Article  >  Web Front-end  >  How to synchronize MongoDB data to MySQL in node.js

How to synchronize MongoDB data to MySQL in node.js

亚连
亚连Original
2018-06-21 17:07:021079browse

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

  • mongoose

  • MySQL

Prepare data in MongoDB

  • For example: What I want to synchronize here is the user table. The user table contains username, email, password...

  • Insert 1000 pieces of data through the MongoDB shell command

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 .htm

Nodejs mysql pool usage example:

mysql module is felixge/node-mysql

The 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;

The key code below

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 (&#39;" + username + "&#39;,&#39;" + email + "&#39;,&#39;" + password + "&#39;);";
 pool.query(sql,(err, rows) => {
  if (err)
  res.send(err); 

  res.json({
  message:&#39;数据插入成功&#39;,
  rows
  });  
 });
 }
});
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn