Use mongoimport to Import a CSV file into a MongoDB Database and Collection

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to go ahead and use the import tool in order to import a csv file into a mongodb database and collection all right so let's go ahead and get into it now i spent about four hours trying to figure out how to do this it was very very frustrating so hopefully this video helps somebody else out there save you know less than four hours to go ahead and import a csv file as a database so i'm i'm brand new to mongodb and yeah so i struggle with this and hopefully this helps somebody else but anyway the first thing of course is hopefully you've downloaded mongodb i'll talk about that real quick so we're on the mongodb website and just i downloaded the community server version so i got mongodb community server just came over here to downloads i did windows now it's likely different on mac or something like that so i hope this may be helpful if you're not on windows but if you're on windows hopefully this video is helpful so on windows go ahead download the file go ahead and install it i did all the you know default settings i did nothing nothing you know customizable in configuring uh the mongodb and then one thing that they don't tell you is you need to come over here to tools and come down here to database tools and you want to go ahead and download this file as well because we're going to need the import file so this import exe this is how we go ahead and import the csv files as a database and this took me forever to figure out um i went and edited the registry i watched 15 youtube videos i went red websites all the way over my head what i needed to do is just download this package right here and that import file is key to importing a csv file as a database there's some other stuff in here no idea what it does maybe down the road if i mess with mongodb some more i'll figure it out but import is the one we need and we'll talk more about it in just a minute here all right so anyway you downloaded you installed mongodb awesome you also downloaded this file right here it's a zip file so you haven't done anything with it quite yet all right now what you need to go ahead and do is navigate to your your mongodb folder so mine you know program files mongodb coming to server click on your your version and then we want to get into this bin area and this is where we're going to want to go ahead and add your import.exe file so and by the way i'll include links to these pages as well so hopefully that'll help you get to them quicker otherwise you saw how i went to it you just went to software and community server and then you can navigate at the top right all right so anyway you're going to want to drag your import exe file from your zip file and put it in into this bin folder and then also you want to go ahead and add in your database files so i have my database.csv file here you throw this in the bin folder as well now there's probably another way to keep things organized other than throwing everything in your bin folder but i don't know how to do that and i didn't bother to look it up because i spent four hours trying to figure out this one thing but by all means if you want to try and figure out other ways to to keep things organized by all means go for it so anyway finally import.exe is in the bin folder your database csv file and i can pop this open real quick so we can take a look so it's pretty simple you know csv file right we got a header that's kind of important so if you have a header that'll be used as like your your column names in your in your table right and then all the the data that's going to go in there all right so simple simple little table all right now we wanted to go to our command line so just cmd and command prompt right here and we need to go ahead and change the directory and we want to change it to this directory up here so it's cd paste that in there hit enter now we're in the program files mongodb server 4.4 bin directory and now what we want to go ahead and do is our little code right here so i have to write this thing out because i don't have this stuff memorized all right so we're going to use the import function feature method whatever you want to call it import and just type this thing yeah so import and then dash d or flag d i think it's technically called so this is um database the database name that we're creating so database two and then dash c for the collection so they call it a collection in mongodb which essentially means table for normal people so collection and we go ahead and name our collection so i'll just call scores and then dash dash type and i'll include this little language down in the description of the video as well csv dash dash file and then the name of our file and then dash dash header line because mine has that header in the csv file right so this is the the code right here i'm going to go ahead and press enter and we see failed open cannot find the specified file so oh let's rename it real quick to database two okay cool and then one thing you can do if you didn't know you just press this up button and it brings back up the the the code you last entered or you press up a bunch of times and see everything you've entered before so save your little time from typing it again so click it run it again and it failed yet again because i can't spell database so okay so that's on me right there database two all right there we go finally so connected to blah blah blah but main thing here is 51 documents imported successfully zero documents failed to import all right awesome that sounds good just to double check things out let's go back into our bin folder and let's go ahead and run mongo.exe right here uh this is how you get to like the command prompt so what i did is i created a shortcut to this file right here and put it on my desktop so i can easily get to it but there's probably other ways to do this i know you can do it through the windows command prompt as well but i don't know i'll just use the command prompt so here i am in the command prompt and now i can do show dbs show databases and we see right here uh database 2 right there so that looks good we'll switch it to use database 2. switch to database 2. and then i want to go ahead and show the collections as well collections so i should see the scores collection because that's what we set up over here right so we had the c scores so that's collection scores i.e our table so there's our scores collection and then we want to go ahead and show the content in there so db co scores dot find and there we go so this is all of our information here that we imported from our spreadsheet so we can look through here and be like yep that looks about right and it does look about right we could go ahead and type it to show more information or more rows that we have i think it lists like 10 at a time or something like that but anyway the point is we're just validating that our information is in here and i've seen there's a different command here i want to go ahead and try it real quick scores um find that pretty see what this does ah so it makes it nice and pretty looking so it's easier to read looks like a json type of thing um but anyway um yeah so hopefully this video helps somebody out there get a csv file imported into mongodb as a database and as a collection i eat a table um hopefully hopefully somebody this helps somebody again i spent like four hours and very very frustrated four hours trying to figure this stupid thing out and hopefully this you know eight minute video help you helped you much quicker alright thank you have a great day
Info
Channel: Becoming a Data Scientist
Views: 43,120
Rating: undefined out of 5
Keywords: becoming a data scientist, becomingads.com, data science, data scientist, mongodb, mongoimport, mongodb csv, mongodb tutorial
Id: nuQD3Xfr0KY
Channel Id: undefined
Length: 8min 47sec (527 seconds)
Published: Fri Sep 25 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.