-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBClient.js
73 lines (63 loc) · 2.52 KB
/
DBClient.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
var mongodb = require('mongodb');
var db;
var dburl;
// export the class
module.exports = DBClient;
// Constructor
function DBClient(url){
this.dburl = url;
}
DBClient.prototype.connect = function(url, callback){
mongodb.MongoClient.open(url, function(err, database){
if(err){
console.error("errorrrrS!:", err);
}else{
console.log("yes!");
this.db = database;
callback(err);
}
});
};
DBClient.prototype.find = function(collection, query, callback){
this.db.collection(collection).find(query).toArray().then(callback(docs));
};
DBClient.prototype.update = function(collection, selector, doc){
// Get the documents collection
var col = this.db.collection(collection);
// do some work here with the database.
//collection.update({mykey:1}, {$set:{fieldtoupdate:2}}, {w:1}, function(err, result) {});
col.update(selector, doc, {upsert: true}, function (err, result) {
if (err) {
console.log(err);
} else {
//console.log('modified %d documents in the ' + collection + ' collection.', result.result.nModified);
console.log('modified %d documents in the ' + collection + ' collection. Failure:', result.result.nModified, result.message.queryFailure, result.message.numberReturned);
//console.log(result.connection);
}
});
/*
MongoClient.connect(dburl, function (err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
// We are connected.
console.log('Connection established to', this.dburl);
// Get the documents collection
var col = this.db.collection(collection);
// do some work here with the database.
//collection.update({mykey:1}, {$set:{fieldtoupdate:2}}, {w:1}, function(err, result) {});
col.update(keydoc, fielddoc, function (err, result) {
if (err) {
console.log(err);
} else {
//console.log('modified %d documents in the ' + collection + ' collection.', result.result.nModified);
console.log('modified %d documents in the ' + collection + ' collection. Failure:', result.result.nModified, result.message.queryFailure, result.message.numberReturned);
//console.log(result.connection);
}
});
//Close connection
this.db.close();
}
});
*/
};