-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.js
106 lines (97 loc) · 2.91 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
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
102
103
104
105
106
var assert = require('assert');
var MongoClient = require('mongodb').MongoClient;
var db
var url = 'mongodb://localhost:27017/PowernetDB';
/**
* Query for one specific collection with condition. If conditioin is {},
* it will return all the document in that collection.
*
* @param {String} collection - The name of the collection
* @param {JSON} condition - The filtering condition
* @param {function} callback - The callback function
* @return void
*/
function query(collection, condition, callback) {
db.collection(collection).find(condition).toArray(callback);
}
/**
* Delete documents in the specified collection which meets the condition
*
* @param {String} collection - The name of the collection
* @param {JSON} condition - The filtering condition
* @param {function} callback - The callback function
* @return void
*/
function del(collection, condition, callback) {
db.collection(collection).deleteMany(condition, callback);
}
/**
* Update documents in the specified collection which meets the condition
*
* @param {String} collection - The name of the collection
* @param {JSON} condition - The filtering condition
* @param {JSON} val - The value to be updated
* @param {function} callback - The callback function
* @return void
*/
function update(collection, condition, val, callback) {
db.collection(collection).updateOne(condition, val, callback);
}
/**
* Insert one record into the specified collection
*
* @param {String} collection - The name of the collection
* @param {JSON} record - The new record in JSON format
* @param {function} callback - The callback function
* @return void
*/
function insertOne(collection, record, callback) {
db.collection(collection).insertOne(record, callback);
}
/**
* Insert a bulk of of records into the specified collection
*
* @param {String} collection - The name of the collection
* @param {Array} records - A list of JSON records to be inserted
* @param {function} callback - The callback function
* @return void
*/
function insertBulk(collection, records, callback) {
var batch = db.collection(collection).initializeUnorderedBulkOp();
for(var i = 0; i < records.length; i++) {
batch.insert(records[i]);
}
batch.execute(callback);
}
/**
* Used to init the mongodb connection. db is used as a connection pool
* to improve the performance. Make sure the node.js server is started in
* the callback function.
*
* @param {function} callback - The callback function
* @return void
*/
function init(callback) {
MongoClient.connect(url, function(err, database) {
assert.equal(null, err);
db = database;
callback();
});
}
exports.init = init
exports.del = del
exports.query = query
exports.update = update
exports.insertOne = insertOne
exports.insertBulk = insertBulk
/**
* One example of how to use the query function.
*
* var mongo = require('./mongo')
*
* app.get('/', function(request, response) {
* mongo.query('homehubs', {}, function(err, results) {
* response.send(results);
* });
* })
*/