Connect Two Tables in MongoDB Using Node & Express

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone and welcome back to daily tuition in this tutorial we are going to understand how to create the second normalized form using mongodb collection with nor and express so let me first explain what is first normalized form let's suppose that you have the author collection in mongodbit database you have author collection or you can say the author model with name bio website title and subtitle fields and you might have your own pills inside this author collection in relational database management system this is what we call a first normalized form table we have all the data of the author with the book details in this type of table or you can say collection you might have duplicate data to solve this you can use second normalized form let's see what is the benefit of using second normalized form in second normalized form the data is consistent in the database so there is no duplicate data in the table it also improves that the overall organization of the data in the database so instead of first normal swamp now we are going to convert this table into second normalized form so to convert it we create two models first is author and second is books in the author we have name bio and website built inside a books collection we create either subtitle and author field you can notice the author field with the author ID the author ID is linked with the author name so when we grab the books detail we can easily access the author ID we don't have primary key in mongodb so let's see how we can create the second normalized form in mongodb no SQL database in a pretty let's dive into the project so I'm going to first open the visual studio code and create a backend server with node and express you can notice here I have a server folder and inside the server I have the server.js file inside this I'm simply going to set up the back end of this application I'm going to import Express create different routes here and connect the mongodb database with this express application so when I start the development server I'm going to first check the connection of the mongodb database and then start the backend server here I have a controller inside that I'm going to have app.controller file we don't have anything here inside the database we have the connection file which used to connect this database to the mongodb then we have the data.js file where we have all the data of the authors so we have all the author details inside this array of object these are that inside this route we have app.rout file and we don't have any route inside this route file and inside is config.js we're simply going to call the mongodb in-memory server to create a simple instance of the mongodb instead of using the mongodb atlas just for this example I'm using mongodb server now just add that let's back to the app controller and create controllers for this application so now once we have a valid mongodb connection let me just start the development server I'm going to open the terminal and here I'm going to say npm start so this command is going to start the development server so you're going to get here a message database connected and server is connected to localhost 8080 now once we successfully started the server let's create model for this application as I said we are going to use second normalized form so instead of creating just the author model in this project we are going to create two different models first for the author and second for the box so inside the server folder here I'm going to create a new folder with the name model and here I'm going to create two files first for the books so I'm going to say book dot model.js you can notice how I name this file book dot model.js this indicates this file is only used for the model these are that inside this model here I'm going to create another file with the name author dot model dot Js inside this book dot model.js right here I'm going to create a schema for the books model as you know to create schema in mongodb you need to First import the Mongoose from the Mongoose model and then you have to call constant book model is equal to and then you call mongos Dot schema and inside this schema you're going to specify fields to this mongodb schema so let's specify fields to this mongodb schema so I'm going to copy and paste here the title field with the type string and we specify here a required message this or that inside a box we also have a subtitle so we call here a subtitle of the type string and after that just add this subtitle here I'm going to call the author field and inside this author field you have to store the author ID I'm going to specify here type this is a type of object so we call here mongos dot schema dot types Dot object so we call here object ID so now the type of this field is a type of mongodb object ID so now when you store the details of the books inside this model we're also going to store the author ID and just for that we hear pass comma and then specify the reference so we call here field called reference and in the single code we pass author right now we don't have this model inside this application after a few seconds I'm going to show you I can create this author model just for now let's save these changes and then just start that just after this object right down here I'm going to call here a comma and call the timestamp this or that right down here you have to export so you have to say here Mongoose dot model and inside a single code we call book we're going to specify name to this model which is book and then we specify structure of it the structure is stored inside this book model so we pass that book model here that's it just are that we also need to specify that if we already have a model then use that default model or create a new one so this statement is going to create a new model if you already have the existing model then use that so we need to specify mongods here dot model Dot books and here I'm going to pass R so if we have existing model then use that otherwise create a new one you can notice here how I use the plural name to this box whenever you create a mongodb collection mongodb is going to specify the plural name to its collection so if you want to access your collection you have to specify the plural name to it to start that let me save the changes back to the author model.js and right here I'm going to create a schema of author so I'm going to first import the mongoose after that I'm going to say here constant author model is equal to Mongoose Dot schema and inside this I'm going to add fields of other model so I'm going to add name bio and website to the name we specify type string so the bio we specify type string and to the website the type is strength just out of that inside this author model right down here we call book spills you can specify any name to this field that's of audio I'm going to specify here books plus 0 comma and specify an add here inside is array you're going to have your box so we specify here an object and specify here type and inside this type you specify Mongoose dot schema dot types Dot object ID and then you specify your comma and say reference of book now you don't have a space over here a plural name to this book's collection now you can notice how I connect both these models using object ID inside this author model I call the reference of the books collection and inside this books model I call the reference of the author collection so inside this author model when I create a new author I can specify the book's ID inside this array the author can have multiple books so inside this box I'm going to store the idea of the book to start that I'm going to just export a default statement and Export the authors now let me say the changes save this file as well and back to the app controller and now let me show you can create the author so I'm simply going to first import the author from the database data.js file so if you open the database data.js file here I have all the authors I'm going to import that authors here just out of that I'm going to import the author model just out of that I'm also going to import the books model I'm going to import the books model here and after that I'm going to say export constant the eighth author and then I'm going to specify here the controller and I'm going to specify using function with request and response parameter and then call here an object something like and inside this I'm going to first call to try and catch block so I'm gonna see here try and catch inside the sketch I'm going to response the status with 404 and then return the message with the error message yes or that inside this try I'm simply going to call the follow-up and iterate over this object so here I'm simply going to call the for Loop just like this and iterate over this object I'm gonna get this object one by one inside this variable I'm gonna call this author and then pass this author object to it and then I'm Gonna Save this author inside a database so I call here new author dot save and at the end I'm gonna respond with the author created message so now this controller is going to create all the authors and store that inside a mongodbit database let me just save the changes and then let me back to the route approve.js file and here I'm going to first import the controllers so I'm going to say here import star asterisk as a controller from and then I'm going to specify here double dot order slash specify controllers app controller.js and after that right here I'm going to say router dot post and then I'm going to create here the end point which is create authors and then I'm going to specify app controller dot create authors so now when you try to access this create author endpoint this is going to create all the authors and store that inside a mongodb database let me just create few more controllers there so I'm going to add create books endpoint show endpoint and author ID endpoint so to get the individual author we can just specify the authoring point with the author ID now let's create all these controllers inside this app.controller file let me just say the changes back to the app controller and minimize this so I'm going to say here export constant create box is equal to a sync request and response and inside this I'm going to call try and catch block inside the sketch I'm going to respond the status code with 404 and inside this try here I'm going to first find the author so what I'm going to do is I'm going to create a variable here constant JK rowlings is equal to a width and from the author from this author model I'm gonna find I'm going to say find one and I'm going to find for the J cable links author so inside an object I'm going to pass JK rowlings take me back to the data.js then you will notice here we have here JK Rowling I'm going to return this object from the state map so this statement is going to return this object inside this variable so if we have this JK rowlings inside the author then we are going to return that object inside this variable and I'm going to do the same for the Tony Robbins as well so I'm going to add here Tony Robbins await author find one Tony Robbins just for that we need to store the books with these authors what we need to do is as you know inside this book's model we need to store the author ID with the books detail as you can notice here we already have the author details right inside these variables so what we're going to do is I'm going to create here a new object so I'm going to say here new book and inside this parenthesis I'm going to call an object and I'm going to pass values to this Fields so once you have your books model you need to specify title subtitle and author ID so inside this object I'm simply going to paste the title The subtitle and the author and what I'm going to do is I'm going to call this JK Rowling's object and get the idea of it and store that inside this author field right here to start that I'm going to do the same and create another book so I'm going to copy and paste a book here something like this and store that object inside a variable so let's do the same for the first object as well just like this or that once we have the books object we can save both these objects so I'm going to call both this variable and call Save method so this statement is going to save this data inside the mongodb database so now this statement is going to create a books model and store all the data inside a mongodbit database now just for that what I want I want to store these books inside the author model as well so if you notice here I have this books of fields inside this I need to specify the object ID I need to specify the reference of the book's collection right down here just after this of it right here we need to call JK Rowling's the variable dot books Dot push and we're going to push this Harry Potter variable right here and we're going to do the same for the second variable as well just like this so I'm going to call both these variables and inside that I'm going to push both this box and after that we need to save both these variables as well so to save this I'm simply going to call await and call the save method and at the end we need to just response the Json object I'm going to say response.json books create that's it now just for that let me back to the app.rout and you will notice here I already have this app controller with this endpoint create a box to create a books inside the mongodb database now just for that let's see how we can show all the books and get the individual author so I'm going back to the app controller minimize this controller and write down here I'm going to say export constant show all is equal to a sync function with request and response parameter just like this and inside this I'm going to call try and cash block inside the sketch I'm going to say response dot status 404 and then return the error message and inside this try here I'm going to say constant authors is equal to of 8 then call the author model I'm going to call here author dot find this find method is going to return all the authors but what I want I also want the books as well with this author so if you open this author model.js then you will notice here we also need to get the books from the books collection so what we can do is here we can call Dot populate and then call here a parenthesis and right here you need to specify the books collection something like this you know to space about the plural name of your collection just are there right down here I can simply return the Json response with this variable that's it and at the end I can simply create here the last endpoint I'm simply going to say here export get author is equal to a sync request and response parameter and then I'm gonna call here try and catch block and inside this sketch right here I'm simply going to return the 404 status and inside this pry here I'm going to say constant author is equal to of it then call the author schema dot find by ID so to get the single user from this author ID you need to call here author dot find ID and inside that I'm going to say request dot params.id now inside this params ID we're going to have the ID of the books so once we get the author inside this author variable we're going to return that from the response dot status we're going to first populate the books as well with this author so I'm going to say here Dot populate and inside this we need to specify the books collection so we call here books as you know we need to specify the plural name of the books inside this populate function this or that right down here we need to call here response dot status and then I'm going to call here status code 200 and the Json response with the author variable so once we have the get author let me just save this file and back to the app route.js and inside this I'm going to have the app controller with the get author controller right here I'm going to call the HTTP get method then specify the author detail whether the author ID and then call the author with the controller now let me just test this API so I'm going to save all the changes save all these files and then I'm going to open this thunder blind using the standard line we are going to test this API let me just clear all these activities and create a new request as you know if I open this terminal you can notice here right down here I Have This Server details let me copy this and specify that right inside this URL and just after that if you're back to the server.js file we need to specify here the API prefix to access the API so I'm going back to the new request and here I'm going to call the powder slash then specify here API and then specify the end points so if I back the app route here we have all the endpoints so let me just first access this show endpoint let me just specify here show and this is a type of get request so let me just specify here get and click on this send button right now if I click on the send button I'm not going to get anything because right now we don't have anything inside this author collection what we need to do is we need to first store the authors and the books and then access all the data of the author and books collection so I'm gonna back to the center client here let me save these changes this will just create this activity log here and we have the get request to start that let me just create a new request so inside this new request I'm going to say here V8 authors I'm going to copy this endpoint please refer that here this is a type of post request so we pass here post and click on send button now when I click on send button you can see we're going to have here a response called authors created now once we have this author created let me create the books as well so if you're back to the app route we have here the endpoint called create box let's copy it and specify that right here now when you click on the send button this is going to create all the books as you can see this is going to create all the books and store all the data inside a mongodb database so now let me just get all the details of the user so let's click on this get request and let me show you all the details now when I click on the send button you can see now we're going to have all the data of the user so when I scroll down we have all the data you can notice here the first object is going to have the books object now inside this book object we have the books details you can notice and we have the books details for the last author so if I scroll down here you can notice for the JK rowlings we have this book's details and if I try to access the individual user so let's suppose that if I make a get request on its author endpoint if I specify here author is a type of get request and then we need to specify the parameter so I need to first get the user detail so let me just back to the previous request copy this user ID right from here and then specify here comma forward slash and then specify this user ID now because we have this ID variable we need to specify value to it so now once we space our value to it this is the type of get request when I click on send button you can see now we're going to have the details of the user as a response so we only going to have a single response from the author you can notice here right inside this object we're going to have the author detail as well as the books detail inside this books array we're going to have the books detail the Tony Robbins might have multiple books so this is a very simple technique to create a second normalized form inside a mongodbit database so if you find anything useful make sure to press the like button share this video with your friends subscribe for more latest videos that is all for now I will see you in the next one [Music] thank you foreign [Music]
Info
Channel: Daily Tuition
Views: 4,365
Rating: undefined out of 5
Keywords: 2NF, second normalize form in express, express second normalize form, create second normalize form in js, working with normalize form, form creation, form, tutorial, how to create a second normalize form, populate collections in mongodb, Connect Two Tables in MongoDB Using Node & Express
Id: zXEYQNP-eQk
Channel Id: undefined
Length: 22min 53sec (1373 seconds)
Published: Mon Feb 06 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.