-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.js
71 lines (60 loc) · 2.09 KB
/
mongo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* Created by chaclus on 2016/11/5.
*/
var mongoose = require('mongoose');
var mysql = require('mysql');
var Schema = mongoose.Schema;
mongoose.connect('mongodb://192.168.3.30/deelive', {keepAlive: 10}, function (err) {
if (err) {
console.error('connect to %s error: ', 'mongodb://192.168.3.30/deelive', err.message);
process.exit(1);
}
console.log("db success")
});
var users = mongoose.model('deeusers',
{
loginid: Schema.Types.ObjectId,
num_revenue: Number,
num_consume: Number
});
var findUserByLoginId = function (loginid, cb) {
// users.find({loginid: new Schema.Types.ObjectId(loginid)}, function (err, data) {
users.findOne({loginid: loginid}, function (err, data) {
if(err) {
console.error(err);
cb(err, null)
}else{
cb(null, data);
}
});
};
var connMysql = function () {
var connection = mysql.createConnection({
host : '192.168.3.30',
user : 'root',
password : 'dev_sql.2016',
database : 'artisan_pay_online'
});
connection.connect();
connection.query('SELECT * from t_user', function(err, rows, fields) {
if (err) throw err;
if(rows && rows.length >0) {
rows.forEach(function (row) {
findUserByLoginId(row.id, function (err, user) {
if(user) {
console.log(user.loginid+" /// "+user.num_revenue + " /// " + user.num_consume);
var sql = "UPDATE t_user set num_revenue=" + user.num_revenue +", num_consume="+user.num_consume +" where id= '"+user.loginid.toString()+"'";
connection.query(sql, function (err, result) {
if(err) {
console.error("update err,", err);
}else{
console.log("change rows : ", result.changedRows);
}
})
}
});
});
}
});
};
connMysql();