Import CSV Data into MongoDB using Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome back in this tutorial I am going to tell you how you can import CSP data to mongodb using python guys suppose we have a CSV file like this and we want to import to mongodb then how we can handle this how we can handle the functionality to import that data so again guys actually first we need to connect to mongodb here actually I have already connected to the mongodb and then need to create a database here we will create like MP data here we have we will create a database MP data and also collection name EMP okay and now we uh our database and collection created here this is this is our database and the collection now we will import data to this so guys uh now we'll code actually here we already created a file a python file import and record CSV dot pya so it's our python file now we will go to import this so actually if I importing the data CSV data we need to to module to import data to mongodb we need two modules one is CSP module to read the CSV data and one other five more modules that we need to use for connecting to the mongodb to import the data so now we will uh start writing code so here we will import CSP module guys remember if you haven't installed this module you need to install actually here we have already installed this here so now you need to use pi module so you need to install this actually we need to use uh client actually we need to connect to the MoGo database so here we have imported two module a CSV module and Pi both these module needed if you have any 8 install this on your system then you need to actually I have already installed here so now we will client the we will create the object of the more clients so we will have variable Moga client and then thank you we will create object so here we have created the object this is the object of our client now we will pass the DB to the mobile client so here we have our DV object and then we will use client object and then pass database actually we have already created here database name EMP data here this is the EMP underscore data so we will call here empty underscore data here so here you have Passover database then we will also pass the collection The Collection is here is EMP so we will pass the collection here so here we have a variable collection and then twist to the collection here so we will create the collection so here our DB object and then we will pass here EMP now we have a collection actually here we have a our database and then we have set the collection so now we have a collection object here now we will perform the operation to insert data to the empty database to MP collections so here now we will move to read the CSV file and insert the record to the mongodb collection so now we have a header variable in which we will pass the headings like name is and country actually these are headings from our CSV file like name is conditioner here we have used these two inside records so now we will have a we will read the CSV file here so we have a CSP file variable and then we will open the CSV file foreign and then read the file so now we have read the file line which join the CSV file object then we have a reader variable and we have a CSV Dot addicted we will dig Theta we will see the CSV file here we have opened the USB file and then we will here read the CSP so data using the CSV object CSV file object so here we have we have read this as we have so we have a data here in editor object now we will look through we will have a follow each in reader and then we have a Arrow and now we will follow on fields in header that we already here Define header and then we have a row field and assign value field now now we have data in row array all field data then we will fill in row actually it will print all data this we are we have read it from the employees USB and then that's going to insert now we'll insert this uh that actually we have our already collection here so we use collection here collection Dot and call the method insert and pass row here to insert the rows so here we have row data each row here and then we insert each row to the collection here so this is our program Python program to inside the data to the mongodb database collection so we have a very brief uh discussion of the code here again here we are importing the CSP module then we are importing the pi mobile using the mobile client so now here we have click here the object of mobile client then we have path the database MP data to mobile client we have object DB then we pass collection EMP to the DB and then we have a collection object here collection and then we have a we are reading here uh CSP file employee CSP file so here we have our headers like the image country then we are we are reading the CSV file employee.csv using the open method then be reading the CSV data using a CSV file object and here we have a object of a file data then we are Loop through the that data and it's storing the the each row in a row area and pass to the collection inside meter actually we are using the collection and calling the method inside to insert the rules now we will run our program here so now we will run our program here you can see here you can see these records are printed actually we have used the print method here so all these require like one two three four all four requires actually here you can so say all these all the one two three four five because so here we have printed the five record like here one two three four five so these calls are here listed now we'll see the data in our mongodb database so here we will load the data here you can see here EMP now all the cars size to a inserted here like five record you can see the five because here one two three four five so all five because imported to the mongodb database MP data to collection EMP so guys you have seen this very interesting to import the data to the mongodb using python just using the CSV and Pi module I hope as you have enjoyed this tutorial and try to application so guys don't forget to like you can share this video and also subscribe our Channel thank you
Info
Channel: WebDamn - A Coders Point
Views: 2,614
Rating: undefined out of 5
Keywords:
Id: sHEdylT0jUQ
Channel Id: undefined
Length: 9min 16sec (556 seconds)
Published: Sun Jan 29 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.