-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial.js
More file actions
51 lines (40 loc) · 1.22 KB
/
tutorial.js
File metadata and controls
51 lines (40 loc) · 1.22 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
const mongo = require("mongodb").MongoClient
const url = "mongodb://localhost:27017/mydb"
/**
* mongo.connect(url, function(err, db) {
if (err) throw err;
console.log("Database created!");
db.close();
})
*/
const findAll = (request, response) => {
mongo.connect(url, function (err, db) {
if (err) throw err;
const dbo = db.db("mydb");
//const myobj = { name: "Teniola Goat", address: "Highway 37", mobile: 081234567 };
dbo.collection("users").find({}).toArray(function (err, res) {
if (err) throw err;
console.log("found!");
db.close();
response.send(res)
});
});
}
const insert = (request, response) => {
mongo.connect(url, function (err, db) {
if (err) throw err
const dbo = db.db("mydb")
const myobj = {
name: request.body.username,
address: request.body.useradd,
mobile: request.body.mobile
}
dbo.collection("users").insertOne(myobj, function (err, res) {
if (err) throw err;
console.log("1 document inserted");
db.close();
response.send(res)
})
})
}
module.exports = { findAll, insert }