-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmongodao.js
65 lines (56 loc) · 1.94 KB
/
mongodao.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
/*
mongo database crud operation handled by nodejs driver
**/
/*
module dependencies
**/
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
function MongoDao(mongoUri, dbname) {
var _this = this;
var options = { useNewUrlParser: true };
_this.mongoClient = new MongoClient(mongoUri, options);
return new Promise(function(resolve, reject) {
_this.mongoClient.connect(function(err, client) {
assert.equal(err, null);
console.log("mongo client successfully connected \n");
_this.dbConnection = _this.mongoClient.db(dbname);
resolve(_this);
});
});
}
MongoDao.prototype.readCollection = function(collectionName) {
return this.dbConnection.collection(collectionName).find();
}
MongoDao.prototype.printDocument = function(collectionName, doc, callback) {
this.dbConnection.collection(collectionName).find({}).filter(doc).toArray(function(err, docs) {
console.log(docs[0]);
console.log("\n");
callback();
});
}
MongoDao.prototype.insertDocument = function(collectionName, doc, callback) {
var _this = this;
this.dbConnection.collection(collectionName).insertOne(doc, function(err, result) {
assert.equal(null, err);
console.log(" Below doc inserted successfully");
_this.printDocument(collectionName, doc, callback);
});
}
MongoDao.prototype.updateDocument = function(collectionName, doc, updateDocument, callback) {
var _this = this;
this.dbConnection.collection(collectionName).updateMany(doc, updateDocument, function(err, result) {
assert.equal(null, err);
console.log(result.result.ok + " document updated successfully");
_this.printDocument(collectionName, doc, callback);
callback();
});
}
MongoDao.prototype.deleteDocument = function(collectionName, doc, callback) {
this.dbConnection.collection(collectionName).deleteOne(doc, function(err, result) {
assert.equal(null, err);
console.log(result.result.ok + " document deleted successfully");
callback();
});
}
module.exports = MongoDao;