-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbHelper.js
More file actions
73 lines (62 loc) · 1.46 KB
/
Copy pathdbHelper.js
File metadata and controls
73 lines (62 loc) · 1.46 KB
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
/**
* Created by addison on 14-6-23.
*
* 数据库操作
* add
* del
* update
* remove
*/
var client = require( 'mongodb' ).MongoClient;
var address = 'mongodb://127.0.0.1:27017/os';
function add( table, data, fn ) {
client.connect( address, function( err, db ) {
db.collection( table ).insert( data, function( err, result ) {
fn && fn( err, result );
db.close();
} )
} )
}
function update( table, fn, data, where ) {
client.connect( address, function( err, db ) {
db.collection( table ).update( where, data, function( err, result ) {
fn && fn( err, result );
db.close();
} )
} )
}
function del( table, fn, where ) {
client.connect( address, function( err, db ) {
db.collection( table ).remove( where || {}, {w: 1}, function( err, result ) {
fn && fn( err, result );
db.close();
} )
} )
}
function select( table, fn, where ) {
client.connect( address, function( err, db ) {
var col = db.collection( table );
col.find( where || {} ).toArray( function( err, result ) {
// console.log(result);
fn && fn( err, result );
db.close();
} )
} )
}
function queryAndUpdata( table, data, fn, where ) {
client.connect( address, function( err, db ) {
var col = db.collection( table );
col.findAndModify( where || {}, [], data, [], function( err, data ) {
fn && fn( err, result );
db.close();
} )
} )
}
var db = {
add: add,
del: del,
update: update,
query: select,
queryAndModify: queryAndUpdata
};
exports.help = db;