-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo-demo.js
165 lines (135 loc) · 3.57 KB
/
mongo-demo.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Database name: EmployeeDB
// Collection name: Employee
// Documents
// {
// {Employeeid : 1, Employee Name : Guru99},
// {Employeeid : 2, Employee Name : Joe},
// {Employeeid : 3, Employee Name : Martin},
// }
const mongoose = require("mongoose");
mongoose.connection.on("error", (err) => {
console.log(err, 'error');
});
const MongoClient = require("mongodb").MongoClient;
MongoClient.connect("mongodb://localhost:27017", (err, client) => {
const db = client.db("angular"); //angular7crud
const collName = "Employee";
if (err) throw err;
db.collections(collName, function (err, names) {
const exist = names.length > 0;
console.log("Exists: ", names.length > 0);
if (!exist) {
db.createCollection("Employee", function (err, res) {
console.log(err, 'err');
if (err) throw err;
console.log("Collection created!");
console.log(res.insertedCount, "works in insertmany");
});
console.log('test');
const userSchema = mongoose.Schema(
{
email: String,
},
{ timestamps: true }
);
const User = mongoose.model("User", userSchema);
}
});
// db.stats().then((res)=>{
// console.log("stats", res);
// })
// db.dropDatabase().then((res)=>{
// console.log("dropDatabase", res);
// })
// db.collection("Employee").insert(
// {
// Employeeid: 5,
// EmployeeName: "NewE",
// },
// function (err, res) {
// console.log(err, 'insert');
// if (err) throw err;
// }
// );
// db.collection("Employee").save(
// {
// Employeeid: 56,
// EmployeeName: "NewEbdf",
// },
// function (err, res) {
// console.log(err, 'save');
// if (err) throw err;
// }
// );
// db.collection("Employee").updateOne(
// {
// EmployeeName: "NewEmployee",
// },
// {
// $set: {
// EmployeeName: "Mohan",
// },
// }
// );
// db.collection("Employee").insertOne(
// {
// Employeeid: 4,
// EmployeeName: "NewEmployee",
// },
// function (err, res) {
// console.log(err, 'insertOne');
// if (err) throw err;
// }
// );
// db.collection("Employee").deleteOne({
// EmployeeName: "Mohan",
// });
// db.collection("Employee").findOne({}, function (err, result) {
// console.log(err, 'result');
// if (err) throw err;
// });
// db.collection("Employee")
// .find({})
// .toArray(function (err, result) {
// console.log(result, err, 'err');
// if (err) throw err;
// });
// db.collection("User").save(
// {
// email: "[email protected]"
// },
// function (err, res) {
// console.log(err, 'User save');
// if (err) throw err;
// }
// );
// db.collection("User")
// .find({})
// .toArray(function (err, result) {
// console.log(result, err, 'User err');
// if (err) throw err;
// });
});
import express from "express"
import dotenv from "dotenv"
import mongoose from "mongoose"
const app=express()
dotenv.config()
const connect = async () => {
try {
await mongoose.connect(process.env.MONGO_TOKEN);
console.log("Connected to Database");
} catch (e) {
throw e;
}
};
mongoose.connection.on("connected", () => {
console.log("mongodb connected");
});
mongoose.connection.on("disconnected", () => {
console.log("mongodb disconnected");
});
app.listen(5000, () => {
connect();
console.log("server listening on port number 5000");
});