DEV Community

Cover image for Importing a csv to MongoDB Atlas on Debian with mongoimport
Benoît Durand
Benoît Durand

Posted on • Updated on

Importing a csv to MongoDB Atlas on Debian with mongoimport

In those times of MongoDB Atlas Hackathon I thought it will be useful to share with the community the process to upload csv/tsv files to MongoDB Atlas service, as it can be use in case a building a data-driven service.

NB : This tutorial is mainly targeted to Debian User.

Pre Requisites :

  • Debian Linux Installed
  • a MongoDB Atlas database cluster

Installing MongoDB Database Tools 🌱

To upload our csv file the first thing we need is to install MongoDB Database Tools on our machine. This will allow us to use the mongoimport tool and bunch of others mongodb related tools such as :

mongodump
mongorestore
bsondump
mongoimport
mongoexport
mongostat
mongotop
mongofiles
Enter fullscreen mode Exit fullscreen mode

To do that you need to select your operating system and download the package here
In case you are like me on Debian or WSL you can directly use one of these commands :

Debian 11

wget https://fastdl.mongodb.org/tools/db/mongodb-database-tools-debian11-x86_64-100.6.1.deb 
sudo apt install ./mongodb-database-tools-debian11-x86_64-100.6.1.deb 

Enter fullscreen mode Exit fullscreen mode

Debian 10

wget https://fastdl.mongodb.org/tools/db/mongodb-database-tools-debian10-x86_64-100.6.1.deb
sudo apt install ./mongodb-database-tools-debian10-x86_64-100.6.1.deb 

Enter fullscreen mode Exit fullscreen mode

And then all the mentionned tools should be installed !

Upload a csv file using the mongoimport command 🌱🌱

The mongoimport command can be use with the following syntax :

mongoimport <options> <connection-string> <file>

Where the connection-string is your mongodb cluster connection string, in the following format :
mongodb+srv://<username>:<password>@<host>/<database>
or
mongodb://<username>:<password>@<host>/<database>

The file arguments is the path of the csv you want to upload, such as imdb_data.csv

Options are bunch of others configurations you can use such as

  • --headerline : To make mongodb use the first line of the csv as the fields reference
  • --collection=<collection_name>: To define which collections to use in your db, otherwise mongo will create one using the name of the csv file

To get a detail of all the options you can refer the help using mongoimport --help

In my case I used the following command to make sure things were done right :

mongoimport \
--uri=mongodb+srv://USERNAME:PASSWORD@HOST \
--db=DB\
--type=csv \
--headerline  \
--DATA.csv \

Enter fullscreen mode Exit fullscreen mode

And that's it ! You can comment below your thought and issues 👇

Top comments (1)

Collapse
 
duranbe profile image
Benoît Durand

Sources and Documentation :