-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmongoFunctions.js
More file actions
36 lines (33 loc) · 1.02 KB
/
mongoFunctions.js
File metadata and controls
36 lines (33 loc) · 1.02 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
const { MongoClient } = require('mongodb');
const config = require("./config");
const uri = `mongodb+srv://${config.config.db.DB_USER}:${config.config.db.DB_PASS}@${config.config.db.DB_HOST}?retryWrites=true&w=majority`;
async function mongoRead(db, collection_name) {
let client = await MongoClient.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true });
var response;
try {
response = await client.db(db).collection(collection_name).find({}).toArray();
}
catch (err) {
console.log(err);
}
finally {
client.close();
}
return response;
}
async function mongoWrite(db, myobj) {
let client = await MongoClient.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true });
var response;
try {
response = await client.db(db).collection("data").insertOne(myobj);
}
catch (err) {
console.log(err);
}
finally {
client.close();
}
return response;
}
exports.mongoRead = mongoRead;
exports.mongoUpload = mongoWrite;