I created this repo , just to save my Mongodb initial starter codes , so that it will be easy for me to access it whenever i feel that i forgot the syntax or want to refer to the code once.
MongoDB atlas for Cloud Database.
M=> Model
V=>Views
C=>Controller
1) Model=> contains the database model / Schema
2) Routes => Contains the Routes like GET,POST,PUT....
3) Controller => contains the logic of the project like create the moviePosts or how to get the posts.
Code :
require('dotenv').config();
const express = require("express");
const app = express();
const mongoose = require('mongoose')
app.use(express.json()); //Middleware : to allow the json res.
// Database Connectivity:
mongoose.connect(process.env.MONGO_DB_URL,{
useCreateIndex : true,
useNewUrlParser : true,
useUnifiedTopology: true,
}).then(()=>console.log("Database Connected Successfully "))
.catch((err)=>console.log(err))
app.listen(process.env.PORT || 5000 ,()=>{
console.log("Server Connected Successfully.");
});
Models Folder :
I have created a file inside this folder called Mens.js
where the Mens runners list are store.
code :
const mongoose = require('mongoose')
const MenSchema = new mongoose.Schema({
ranking : {
type: Number,
require: true, // this ranking field is REQUIRED ,else ERROR.
unique:true // unique !
},
name : {
type: String,
require: true,
trim:true // means it will trim the SPACES
},
dob : {
type: Date,
require: true,
trim:true
},
country : {
type: String,
require: true,
trim:true
},
score : {
type: Number,
require: true,
trim:true
},
event : {
type: String,
default: "100m" // means byDefault set to 100 Meter if not provided the value.
},
});
// we are creating a new Collection.
const MensRanking = new mongoose.model('MenRanking',MenSchema)
module.exports = MensRanking
Now , Lets Configure the Router Folder
Code : Mens.js
const Router = require('express').Router();
const MensRankingModel = require('../Models/Mens');
Router.post('/', async(req,res)=>{
try {
const mensRecord = new MensRankingModel(req.body);
const docResponse = await mensRecord.save();
console.log(res.body);
res.json(docResponse)
} catch (error) {
console.log(error)
res.status(400).json(error)
}
})
module.exports = Router;
So we have to make some changes to our Index.js
file : so,
Code :
require('dotenv').config();
const express = require("express");
const app = express();
const mensRoute = require('./Routers/Mens.js')
app.use(express.json()); //Middleware : to allow the json res.
const mongoose = require('mongoose');
// Database Connectivity:
mongoose.connect(process.env.MONGO_DB_URL,{
useNewUrlParser : true,
useUnifiedTopology: true,
}).then(()=>console.log("Database Connected Successfully "))
.catch((err)=>console.log(err))
// using the Router from Routers folder.
app.use('/mens',mensRoute);
app.listen(3000 ,()=>{
console.log("Server Connected Successfully.");
});
Testing the REST API using Postman tool :
(Note: the data should match the fields we have created in our schema. Fields can be also be 1 or 2 but should not more than the Defined Model schema.)
RESPONSE :
MongoDB Atlas Database Response :
Now Similarly , create other routes like GET PUT PATCH DELETE ..
in the Router folder
^-^ Happy Coding ^-^