DEV Community

Ali
Ali

Posted on

How to import csv data in mongoDB

Question: How to add CSV data in mongoDB Atlas or local mongoDB?
Answer:
1) MongoDB command line utility mongoimport is used to import csv/json data in mongoDB. It is bundled in MongoDB Database Tools package which can be downloaded by vising Tools section on mongoDB official website. Here is the link to download this https://www.mongodb.com/try/download/database-tools

2) After installing to your machine to the install folder and run the following command
mongoimport --uri "mongodb+srv://username:password@cluster0.xxxxx.mongodb.net/database_name?retryWrites=true&w=majority" --collection=collection_name --mode=insert --type=csv --headerline --file=d:\data\userlist.csv

Replace username, password, database_name, collection_name and --file with your own values. Also replace cluster0.xxxxx.mongodb.net with your own cluster address. Full database connect string including cluster address can be found on Database page by pressing Connect button. --mode=insert will append data into your collection. --headerline will tell mongo that first line contains field names. For full mongoimport options please visit official documentation at https://www.mongodb.com/docs/database-tools/mongoimport/#csv-import

For local mongoDB instance instead of --uri, --hostname can be used. Syntax will be like --host=<:port>

Top comments (0)