-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertquestion.js
41 lines (34 loc) · 1017 Bytes
/
insertquestion.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
const { MongoClient } = require("mongodb");
module.exports = async function (title, area, detail, tags) {
const uri = process.env.DB_CREDENTIALS;
const client = new MongoClient(uri);
let successOrFail = false;
try {
await client.connect();
const database = client.db("4learn");
const collection = database.collection("userQuestion");
// create js obj received by /register
const doc = {
title: title,
area: area,
detail: detail,
tags: {
oAlevel: tags.oAlevel,
subject: tags.subject,
year: tags.year,
paperNumber: tags.paperNumber,
month: tags.month,
questionNumber: tags.questionNumber,
},
};
const result = await collection.insertOne(doc);
console.log(`${result.insertedCount} documents were inserted into db`);
console.log(result.insertedCount > 0);
if (result.insertedCount > 0) {
successOrFail = true;
return true;
}
} finally {
await client.close();
}
};