-
Notifications
You must be signed in to change notification settings - Fork 3
/
db.js
49 lines (42 loc) · 1.3 KB
/
db.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
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/msgBoard';
var documentName = 'msg';
function save(data, callback) {
MongoClient.connect(url, function(err, db) {
if ( err ) {
console.log(err);
} else {
var collection = db.collection(documentName);
collection.insert(data, function(err, result) {
if ( err ) {
console.log(err);
} else {
console.log('insert success!!');
callback && callback(result);
db.close();
}
});
}
});
}
function list(page, callback) {
var pageNum = 15;
var skipNum = (page - 1) * pageNum;
MongoClient.connect(url, function(err, db) {
if ( err ) {
console.log(err);
} else {
var collection = db.collection(documentName);
collection.find({}).skip(skipNum).limit(pageNum).sort({dtime: -1}).toArray(function(err, result) {
if ( err ) {
console.log(err);
} else {
callback && callback(result);
db.close();
}
});
}
});
}
exports.save = save;
exports.list = list;