-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpressMongoDb.js
74 lines (62 loc) · 1.47 KB
/
expressMongoDb.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var mongoose = require('mongoose')
var Customer = require('./schemas/customers.js')
function Connect() {
mongoose.connect('mongodb+srv://nodespace:[email protected]/?retryWrites=true&w=majority', (error) => {
if (!error) {
console.log('Connected To The MongoDB')
}
else{
throw error;
}
})
}
function Create(name, city) {
var newCustomer = new Customer({
name: name,
city: city
});
newCustomer.save((error) => {
if (error) {
throw error;
}
console.log("New Customer Succesfuly Saved.")
});
}
function Read(id) {
Customer.findById(id, (error, data) => {
if (error) {
throw error;
}
console.log(data);
})
}
function Update(id, name, city) {
Customer.findById(id, (error, data) => {
if (error) {
throw error;
}
console.log(data);
data.name = name
data.city = city
data.save((error) => {
if (error) {
throw error;
}
console.log("Customer updated")
})
})
}
function Delete(id) {
Customer.findById(id, (error, data) => {
if (error) {
throw error;
}
console.log(data);
data.remove((error) => {
if (error) {
throw error;
}
console.log("Customer Deleted.")
})
})
}