PUT API Update data in MongoDB
This blog and in upcoming blog we will see
mongodb basic and how is it work with installation and after
installation we should not face
any problem while making Database, collection(table) and using the data in nodejs even whenever we will make
API. Read data from mongodb in nodejs
- # Make PUT method for API
-
# Send data from postman
-
# Handle data in nodejs by request
- # Write code for update data in MongoDB
Now in this blog we will consider about mongoDB basic command which we can use
to create Database, collection etc.
What is MongoDB
MongoDB is a popular open-source NoSQL (non-relational) database management system. NoSQL means no structure like it is look like an object while in SQL it is a structure query language like SELECT CustomerName, City FROM Customers; (The SELECT statement is used to select data from a database.) But NoSQL database is useless...NOT completely because of whenever we want add extra column in database then it is possible through NoSQL MongoDB.
MongoDB installation - CLICK HERE
MongoDB Basic Command -
CLICK HERE
About MongoDB vs SQL - CLICK HERE
CRUD in MongoDB - CLICK HERE
Object Like -
{ "_id": { "$oid": "659ad46e0382148123c82841" }, "name": "moto G60", "brand": "motorola", "price": 16500, "category": "mobile" }
MongoDB is a type of database that helps store and manage large amounts
of data in a flexible and scalable way. Unlike SQL, MongoDB doesn't require a
fixed structure for the data, allowing you to store information in a more
versatile format.
- MongoDB is a NoSQL database.
- The data stored in a collection
- Collection don't have row and columns
- Data is stored in the form of object.
Make new file for API
If you remember, we wrote the code for database connection and data reading in the same file. if you write coding of insert, update, delete in same function and same file then it will be very complex. so that's why we will separate file of mongoDB connection file. Like this -
mongodb.js
const { MongoClient } = require('mongodb'); const uri = 'mongodb://127.0.0.1:27017/'; const client = new MongoClient(uri); const dbName = 'e-comm'; async function dbConnect() { //handle promises let result = await client.connect(); console.log(result) db = result.db(dbName); return db.collection("products") } module.exports = dbConnect;
Make PUT method for API
Here we are updating data in our MongoDB database by PUT method. So, first we simply add put method with resp.send({result : "updated"}).
app.put('/', (req, resp) => { resp.send({result : "updated"}) })
After running this code with nodemon api.js and check in postman.
if we get the data from postman body with the code (req.body) you can simply add the JSON code in postman body section and in our code we will add console.log(req.body) .
app.put('/', (req, resp) => { console.log(req.body) resp.send({result : "updated"}) })
Now with static data we can change the price in mongoDB through postman. Like this
Before -
app.put('/', async (req, resp) => { let data = await dbConnect(); let result = data.updateOne( {name : "samsung m40"}, { $set : {price : 4444}} ) resp.send({result : "updated"}) })
Go to postman and hit send button and check in mongoDB
After -
Now with the dynamic data we can change the price (whatever you want to change) so simple add the {$set : req.body} and send the data by postman.
app.put('/', async (req, resp) => { let data = await dbConnect(); let result = data.updateOne( {name : "samsung m40"}, { $set : req.body} ) resp.send({result : "updated"}) })
Whole Code
const express = require("express"); const dbConnect = require("./mongodb/mongodb"); const app = express(); app.use(express.json()); app.put("/", async (req, resp) => { let data = await dbConnect(); let result = data.updateOne({ name: "samsung m40" }, { $set: req.body }); resp.send({ result: "updated" }); }); app.listen(5000);
Disclaimer
All tutorials are for informational and educational purposes only and have been made using our own routers, servers, websites and other vulnerable free resources. we do not contain any illegal activity. We believe that ethical hacking, information security and cyber security should be familiar subjects to anyone using digital information and computers. Hacking Truth is against misuse of the information and we strongly suggest against it. Please regard the word hacking as ethical hacking or penetration testing every time this word is used. We do not promote, encourage, support or excite any illegal activity or hacking.
0 comments:
Post a Comment
For Any Tech Updates, Hacking News, Internet, Computer, Technology and related to IT Field Articles Follow Our Blog.