DEV Community

Cover image for Learning data modelling from chai aur code
Rahul
Rahul

Posted on

Learning data modelling from chai aur code

Hello! I am currently learning backend web development from you-tube channel chai aur code has a series with name chai aur backend. Here first i learned how to connect backend with frontend where sir told us about CORS (error cross origin resource sharing) and how to solve it without using any npm package, Next i learned about data modelling which is the blueprint of how the data is saved in mongodb database for this we used mongoose odm(object data modelling) which has mehods used to create model schema using mongoose.schema imported from mongoose. Here we have to take care of one thing while creating schema from mongoose which is new keyword used in js to create new Object. In making schema we have to give properties to field according to use case like most of the people have name in string (11)so full name and username should be String but username should be unique according to use case. After writing fields now the next step is to create the model from mongoose.model() which take two arguments first is the name of model and the another one is the schema that we created. The mongodb change the name of model when it saves it for example if i give my model name "User" mongodb save it as "users". So this is also one more important thing. Now for making models from mongoose i have to use the below written code:-


import mongoose from "mongoose"

const schema_Name= new mongoose.Schema({model_fields},{timestamps_})

const modelName = mongoose.model("modelName",schema_Name)

Enter fullscreen mode Exit fullscreen mode

timestamps:true is given to get createdAt and updatedAt field in the model/collection in database. so this is the boiler plate code now we just only need to think structure and data modelling of model and add object in model field.

Top comments (0)