Node JS Tutorial in Hindi #53 | Connect Node.js with MongoDB Using Mongoose

Connect MongoDB with Mongoose


Points of video:

  1. Install Mongoose npm package
  2. Connect Node with MongoDB
  3. Write schema for collection
  4. Fetch data and check output
  5. Interview Questions
  6. 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