node.js將MongoDB數(shù)據(jù)同步到MySQL的步驟
前言
最近由于業(yè)務(wù)需要,APP端后臺(tái)需要將MongoDB中的數(shù)據(jù)同步到Java端后臺(tái)的MySQL中,然后又將MySQL中算好的數(shù)據(jù),同步到MongoDB數(shù)據(jù)庫(kù)。
這個(gè)過(guò)程看是很繁瑣,實(shí)際上這就是一個(gè)互相寫(xiě)表的過(guò)程。
接下來(lái)就看看node.js將MongoDB中的數(shù)據(jù)批量插入到MySQL數(shù)據(jù)庫(kù)的實(shí)現(xiàn)過(guò)程。話(huà)不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。
環(huán)境
- node.js
- MongoDB
- MySQL
- npm
需要的模塊
- mongoose
- MySQL
準(zhǔn)備好MongoDB中的數(shù)據(jù)
- 比如說(shuō):我這里要同步的是用戶(hù)表,用戶(hù)表中包含username,email,password...
- 通過(guò)MongoDB shell命令插入1000條數(shù)據(jù)
實(shí)現(xiàn)
mongoose的Schema我這里就不寫(xiě)了,大家可以上網(wǎng)進(jìn)行查看,node.js連接MongoDB和MySQL的pool看下面:
node.js連接MongoDB:http://www.dhdzp.com/article/98813.htm
Nodejs mysql pool使用實(shí)例:
mysql模塊為felixge/node-mysql
源碼如下:
/**
* 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 : '站點(diǎn)分布', results : rows});
//回收pool
connection.release();
});
});
});
module.exports = router;
下面上關(guān)鍵代碼
思路:
先從MongoDB查詢(xún)數(shù)據(jù)然后通過(guò)遍歷插入MySQL中。
User.find({}, (err, user) => {
if (err)
res.send(err);
for( let i = 0 ; i < family.length ; i ++ ) {
console.log("第" + (i + 1) + "條數(shù)據(jù)");
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:'數(shù)據(jù)插入成功',
rows
});
});
}
});
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- Python實(shí)現(xiàn)將數(shù)據(jù)框數(shù)據(jù)寫(xiě)入mongodb及mysql數(shù)據(jù)庫(kù)的方法
- 記一次MongoDB性能問(wèn)題(從MySQL遷移到MongoDB)
- Python中MySQL數(shù)據(jù)遷移到MongoDB腳本的方法
- MongoDB與MySQL的操作對(duì)比表及區(qū)別介紹
- MongoDB與MySQL常用操作語(yǔ)句對(duì)照
- MongoDB系列教程(五):mongo語(yǔ)法和mysql語(yǔ)法對(duì)比學(xué)習(xí)
- python連接MySQL、MongoDB、Redis、memcache等數(shù)據(jù)庫(kù)的方法
- mongodb與mysql命令詳細(xì)對(duì)比
- 基于MySQL到MongoDB簡(jiǎn)易對(duì)照表的詳解
- MySQL和MongoDB設(shè)計(jì)實(shí)例對(duì)比分析
- 分析MongoDB和MySQL各自的關(guān)鍵特性、差別和優(yōu)勢(shì)
相關(guān)文章
nodejs實(shí)現(xiàn)連接mongodb數(shù)據(jù)庫(kù)的方法示例
這篇文章主要介紹了nodejs實(shí)現(xiàn)連接mongodb數(shù)據(jù)庫(kù)的方法,結(jié)合實(shí)例形式分析了nodejs針對(duì)mongodb數(shù)據(jù)庫(kù)的簡(jiǎn)單連接、查詢(xún)及關(guān)閉等操作技巧,需要的朋友可以參考下2018-03-03
用nodeJS搭建本地文件服務(wù)器的幾種方法小結(jié)
本篇文章主要介紹了用nodeJS搭建本地文件服務(wù)器的幾種方法小結(jié),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03
npm ERR! code ELIFECYCLE錯(cuò)誤及解決方法
有時(shí)候在使用npm運(yùn)行命令時(shí),可能會(huì)遇到錯(cuò)誤,本文主要介紹了npm ERR! code ELIFECYCLE錯(cuò)誤及解決方法,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05

