Skip to content
All writing
  • MongoDB
  • Excel
  • MERN

A Simple way to Import Excel to Mongo Atlas

Import Excel records into MongoDB Atlas for a MERN library application.

Asela Priyadarshana2 min read

This article was originally posted on LinkedIn.

A few days ago, I was working on developing a library system for my school using a MERN stack.

During this project, I had to import around 12,000 excel records into my Mongo Atlas database because library staff already maintained their book and student information in excel sheets.

When looking for a way to import this data into a Mongo Atlas cluster, I found a tool called "mongoimport."

Mongoimport is a MongoDB database tool that was released as a standalone tool after MongoDB 4.4. If you are using a higher version than Mongo 4.4, you have to install it.

Hense installation is different for each operating system please follow the official guide for installation.

With this tool, I was able to import data into my database by following the steps described below.

First, I deleted all unnecessary columns and titles in the excel sheet. Mongoimport commands allow you to give column titles with the appropriate types.

After that, I saved my file as a CSV file; I used LibreOffice, but you could also use Google Sheets if you prefer.

Now we are ready to import our file to the mongo database.

Please keep in mind that if you use unique key validation in the collection and haven't created a collection yet, I recommend that you should manually add at least one document to your collection. Otherwise, validations will not work properly while importing a CSV file.

In the below example, I used two-column "Name, Barcode." with their types, More types in Mongo can be found in mongoimport documentation.

Also don't forget to update <connecttionString>,<collectionName>, <CSVpath> with your own values.

mongoimport --uri <connecttionString> --collection <collectionName> --type csv --file <CSVpath> -f "Name.string(),Barcode.string()" --columnsHaveTypes 

Enclosing fields in double quotes will prevent shell errors.

View the original article and illustration on LinkedIn.

Done! Isn't it simple? You can use this technique to import Excel into MongoDB starting today.

Written by Asela PriyadarshanaFull-stack & AI engineer in Sri Lanka. I write about what I build and learn.
About me
Keep reading