How to Import a CSV into a Collection using the MongoDB Shell
Introduction
If you’re transitioning to using MongoDB you might be wondering how you move your existing CSV data into MongoDB collection. This short tutorial will show you how to do it step-by-step. It’s a very important process but luckily the process is not complicated so please follow along as we show you how.
Prerequisites
- For this tutorial you should have MongoDB installed and running.
- It would also be helpful if you had CSV file of your data with headers. If you don’t though we’ll provide sample of csv data which you can use to create a csv file.
Goal
Our goal for this tutorial is to import data from our people.csv
file below into the default test
Database.
Let’s first take a look at the data in our people.csv
1 2 3 4 5 6 | First Name,Last Name,Gender,Age Alfred,Amus,Male,39 Bob,Caster,Male,65 Caitlyn,Anderson,Female,21 Donna,Dee,Female,21 Tessa,Sanders,Female,39 |
1. Use mongoimport
Use the mongoimport command with the following flag options:
1 2 3 4 | --collection <name_of_collection> --file <path_to_csv_file> --type csv --headerfile |
Let’s discuss each flag:
–file: Tells MongoDB where to find your csv file. Our file is in our current working directory. If your csv file isn’t in your current working you’ll have to use either the relative or full path to your csv file.
–type: Specifies that our data is in the comma separated value format.
–headerfile: Indicates to use the first row as the names the key values.
Note: Use mongoimport –help to get more info about the command.
1 | $ mongoimport --collection people --file people.csv --type csv --headerline |
Now you should receive a success message stating the number of documents that were imported.
1 2 | > connected to: localhost > imported 5 documents |
2. Validate the Import
Now to validate that the data was formatted as desired, strings are strings, numbers are numbers, dates are dates. To do that select test
database, then view it’s records using .find() method with .pretty() to view in a readable format.
Note: Don’t worry about printing too many records it will limit you to twenty by default.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | > mongo > use test > db.people.find().pretty() { "_id" : ObjectId("5c783a0cad685885625aaf48"), "First Name" : "Tessa", "Last Name" : "Sanders", "Gender" : "Female", "Age" : 39 } { "_id" : ObjectId("5c783a0cad685885625aaf49"), "First Name" : "Donna", "Last Name" : "Dee", "Gender" : "Female", "Age" : 21 } { "_id" : ObjectId("5c783a0cad685885625aaf4a"), "First Name" : "Caitlyn", "Last Name" : "Anderson", "Gender" : "Female", "Age" : 21 } { "_id" : ObjectId("5c783a0cad685885625aaf4b"), "First Name" : "Alfred", "Last Name" : "Amus", "Gender" : "Male", "Age" : 39 } { "_id" : ObjectId("5c783a0cad685885625aaf4c"), "First Name" : "Bob", "Last Name" : "Caster", "Gender" : "Male", "Age" : 65 } |
We can see that our data was correctly imported. The strings were correctly imported as strings and the Age was correctly imported as a number.
For Other Use Cases
For other use cases like importing into an existing database, importing into an existing collection, or for import from other data file types besides csv, consult the MongoDB documentation. You can alse use the bash command to see all the options available. * mongoimport –help
Conclusion
In this tutorial we showed you how to import data in a csv file into a MongoDB collection. In this example it created an entirely new collection people
which did not exist before. We imported it into the default test
database. We had to use three flags on the mongoimport command which specified the details of the import. We hope you were able to follow along and that this was helpful in your specific application.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started