const mysql = require('mysql');
const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://<YOUR_CONNECTION_STRING>";
var config = {
mysql_pool : mysql.createPool({
host : '<HOST>',
user : '<USER>',
password : '<PASS>',
database : '<DB>'
});
};
var mySqlPromise = new Promise(function(resolve,reject){
config.getConnection(function (err, connection) {
connection.query('{YOUR QUERY}' ,[{FIELDS}], function (err, rows) {
if(err){
reject(err);
}else{
resolve(rows);
}
connection.release();
});
});
});
var mongoPromise = new Promise(function(resolve,reject){
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("mydb");
dbo.collection("<YOURCOllECTION>").findOne({<Query>}, function(err, result) {
if (err) reject(err);
resolve(result);
db.close();
});
});
});
Promise.all([mySqlPromise, mongoPromise],function(result){
//here result[0] is mysql result and result[1] is mongo result do your computation
}).catch(function(err){
//in case of error code here
});
1条答案
按热度按时间muk1a3rh1#
连接nodejs中的两个db之后。您只需使用promise查询单个数据库,然后使用promise即可。在获取两个结果后,您就可以比较字符串了。示例代码如上所示。