-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmongo-play.js
More file actions
51 lines (42 loc) · 1.39 KB
/
Copy pathmongo-play.js
File metadata and controls
51 lines (42 loc) · 1.39 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
const assert = require('assert');
const dbHandler = (err, dbClient) => {
if (err) {
console.log("Database not connected!");
//throw err;
}
else {
console.log("Database connected!");
}
};
class mongoPlay {
constructor(localDbHandler, dbName) {
this.currDb = localDbHandler.db(dbName);
}
dbInsertOne = (collName, doc) => {
this.currDb.collection(collName).insertOne(doc, (err, result) => {
assert.equal(null, err);
assert.equal(1, result.insertedCount);
console.log('One document inserted!');
});
};
dbInsertMany = (collName, docs) => {
this.currDb.collection(collName).insertMany(docs, (err, result) => {
assert.equal(null, err);
assert.equal(docs.length, result.insertedCount);
console.log(`${result.insertedCount} document(s) inserted!`);
});
};
dbFind = (collName, match = {}, rlimit = 1, callback, ...extParams) => {
this.currDb.collection(collName).find(match).limit(rlimit).toArray((err, docs) => {
assert.equal(null, err);
assert.ok(docs !== null);
console.log(`${docs.length} document(s) found!`);
callback(docs);
});
};
}
module.exports = {
dbHandler: dbHandler,
mongoPlay: mongoPlay,
moduleInfo: { name: 'Mongos Play', version: '1.0-beta' }
};