-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
36 lines (35 loc) · 1.13 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
var MongoClient = require('mongodb').MongoClient;
var url = "<Mongo URL DB HERE>";
module.exports = {
/**
* Insert to collection "chatLog" on MongoDB server
*/
insertChat: function (data) {
MongoClient.connect(url, function(err, db) {
var database = db.db('<db name>');
if (err) throw err;
var myobj = { username: data.username, content: data.content };
database.collection("chatLog").insertOne(myobj, function(err, res) {
if (err) throw err;
console.log("1 document inserted");
db.close();
});
});
},
/**
* Find all data in chatLog collection
*/
findChat: function (callback) {
var chatLog = [];
MongoClient.connect(url, function(err, db) {
var database = db.db('<db name>');
if (err) throw err;
database.collection("chatLog").find({}).toArray(function(err, result) {
if (err) throw err;
console.log(result);
callback(result);
db.close();
});
});
}
};