Skip to content

shashwat2910/CRPD-Nodejs-MongoDB

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CRUD-Nodejs-MongoDB

CRPD operation through API in Nodejs connected to MongoDB Atlas
C - Create
R - Read
P - Patch
D - Delete

Overview Of the API we are coding

Methods Urls Actions
POST /adduser Create new User
GET /users Get all Users Data
PATCH /user/:id Update a User by id
DELETE /user/:id Delete a User by :id

How to run the Project ?

  • Git Clone the Project
  • In the main directory run this command
nodemon server.js

Packages to download

npm i mongoose
npm i nodemon

Connection to MongoDB Atlas

  • Create a file named server.js
  • Paste this code given below
const mongoose = require("mongoose")

mongoose.connect(
  "mongodb+srv://<username>:<password>@cluster0.04hy5.mongodb.net/<database>?retryWrites=true&w=majority",
  {
    useNewUrlParser: true,
    useUnifiedTopology: true
  }
)

Model for the document

  • Create a file named user.js
  • Paste this code given below
const mongoose = require("mongoose")

const UserSchema = new mongoose.Schema({
  name: {
    type: String,
    require: true
  },
  email: {
    type: String,
    unique: true,
    require: true
  },
  password: {
    type: String,
    require: true
  }
})

const User = mongoose.model("User", UserSchema)

module.exports = User

API's
CodingGIF

POST Request

app.post("/adduser", async (request, response) => {
  const user = new User(request.body)
  try {
     await user.save()
  } catch (error) {
    response.status(500).send(error)
  }
 })

GET Request

app.get("/users", async (request, response) => {
  const users = await User.find({})
  try {
    response.send(users)
  } catch (error) {
    response.status(500).send(error)
  }
})

PATCH Request

app.patch("/user/:id", async (request, response) => {
  const updates = Object.keys(request.body)
  try {
    const id = request.params.id
    const updatedData = request.body
    const options = { new: true }
    const result = await User.findByIdAndUpdate(
      id, updatedData, options
    )
    response.send(result)
  } catch (error) {
    response.status(500).send(error)
  }
})

DELETE Request

app.delete("/user/:id", async (request, response) => {
  try {
    const user = await User.findByIdAndDelete(request.params.id)
    if (!user) response.status(404).send("No user found")
    response.status(200).send()
  } catch (error) {
    response.status(500).send(error)
  }
})

If you have any doubts or errors please feel free to connect me on Linkedin Email

About

CRUD operation through API in Nodejs connected to MongoDB Atlas

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published