Node JS Tutorial in Hindi #53 | Connect Node.js with MongoDB Using Mongoose
Connect MongoDB with Mongoose
Points of video:
- Install Mongoose npm package
- Connect Node with MongoDB
- Write schema for collection
- Fetch data and check output
- Interview Questions
- Notes, Code, and Playlist
index.js File code
import mongoose from 'mongoose'
import express from 'express'
import studentModel from './model/studentModel.js';
const app = express();
await mongoose.connect("mongodb://localhost:27017/school").then(()=>{
console.log("_______connected______");
})
app.get("/",async (req,resp)=>{
const studentData= await studentModel
resp.send(studentData)
})
app.listen(3200)
// async function dbConnection(){
// await mongoose.connect("mongodb://localhost:27017/school");
// const schema= mongoose.Schema({
// name:String,
// email:String,
// age:Number,
// })
// const studentsModel = mongoose.model('students',schema);
// const result = await studentsModel.find();
// console.log(result);
// }
// dbConnection();
schema/studentSchema.js file code
import mongoose from "mongoose";
const studentSchema= mongoose.Schema({
name:String,
age:Number,
email:String
})
export default studentSchema;
github Link :- https://github.com/anil-sidhu/node-express-mongodb/tree/node-with-mongoose