-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.js
102 lines (96 loc) · 2.48 KB
/
connection.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = 'mongodb://localhost:27017/anglo';
var insertData = function(db, callback) {
//插入数据
var data = [{"name":'wilson001',"age":21},{"name":'wilson002',"age":22}];
collection.insert(data, function(err, result) {
if(err)
{
console.log('Error:'+ err);
return;
}
console.log("插入成功");
callback(result);
});
}
var selectData = function(db, callback) {
var collection = db.collection('anglo_coll');//连接到表
//查询数据
var whereStr = {};
collection.find(whereStr).toArray(function(err, result) {
if(err)
{
console.log('Error:'+ err);
return;
}
console.log("查询成功");
callback(result);
});
}
var updateData = function(db, callback) {
var collection = db.collection('anglo_coll');//连接到表
//更新数据
var whereStr = {"name":'wilson001'};
var updateStr = {$set: { "age" : 100 }};
collection.update(whereStr,updateStr, function(err, result) {
if(err)
{
console.log('Error:'+ err);
return;
}
console.log("更新成功");
callback(result);
});
}
var delData = function(db, callback) {
//连接到表
var collection = db.collection('anglo_coll');
//删除数据
var whereStr = {"z":1};
collection.remove(whereStr, function(err, result) {
if(err)
{
console.log('Error:'+ err);
return;
}
console.log("删除成功");
callback(result);
});
}
var invokeProcData = function(db, callback) {
//存储过程调用
db.eval('get_tb2count()', function(err, result) {
if(err)
{
console.log('Error:'+ err);
return;
}
console.log("存储过程调用");
callback(result);
});
}
var get_tb2_count = function(db, callback){
//连接到表
var collection = db.collection('anglo_coll');
var counter = collection.find({}).count();
console.log(counter)
}
MongoClient.connect(DB_CONN_STR, function(err, db) {
// updateData(db, function(result) {
// db.close();
// });
// selectData(db, function(result) {
// console.log(result);
// db.close();
// });
delData(db, function(result) {
db.close();
});
selectData(db, function(result) {
console.log(result);
db.close();
});
get_tb2_count(db, function(result) {
db.close();
});
});