-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrud_using_mongodb.js
53 lines (45 loc) · 1.44 KB
/
crud_using_mongodb.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
const express = require('express');
const app = express()
const { MongoClient } = require('mongodb');
const url = 'mongodb://localhost:27017/';
const client = new MongoClient(url)
async function dbConnection() {
let result = await client.connect()
let db = result.db('E-commerce')
let collName = db.collection('products')
return collName;
}
const port = process.env.PORT || 8000
app.use(express.json());
//1. Create/Insert/Post
app.post('/products', async (req, res) => {
let database = await dbConnection()
let response = await database.insertMany(req.body);
res.send(response)
})
//2. Read/Get
app.get('/products', async (req, res) => {
let database = await dbConnection()
let response = await database.find().toArray();
res.send(response)
})
app.get('/products/:os', async (req, res) => {
let database = await dbConnection()
let response = await database.find({ os: req.params.os }).toArray();
res.send(response)
})
//3. Update/Put
app.put('/products/:os', async (req, res) => {
let database = await dbConnection()
let response = await database.updateOne({ os: req.params.os }, { $set: req.body })
res.send(response)
})
//4. Delete
app.delete('/products/:os', async (req, res) => {
let database = await dbConnection()
let response = await database.deleteOne({ os: req.params.os })
res.send(response)
})
app.listen(port, () => {
console.log(`server is listening on the port ${port}`)
})