How to Import CSV and JSON file in Mongo Atlas & MongoDb

Syntax

mongoimport 
  --host cluster0-shard-00-02.c0uc9.mongodb.net:27017 
  --ssl --username adminprabha
  --password ***1**
  --authenticationDatabase admin 
  --db bookmark
  --collection bookmarks
  --type json
  --file mybookmark.json

cluster0-shard-00-02.c0uc9.mongodb.net:27017

Download Database tool

Extract the file and create a new bin folder into the MongoDB bin folder ( C:\Program Files\MongoDB\Server\4.4\bin )

Exectue the command :

Import JSON file

> mongoimport --host cluster0-shard-00-02.c0uc9.mongodb.net:27017 --ssl --username adminprabha 
--password ***1** --authenticationDatabase admin --db bookmark --collection bookmarks 
--type json --file C:\Users\Admin\Desktop\mybookmark.json

Import CSV file

> mongoimport --host cluster0-shard-00-02.c0uc9.mongodb.net:27017 --ssl --username adminprabha 
--password ***1** --authenticationDatabase admin --db bookmark --collection bookmarks 
--type csv --file C:\Users\Admin\Desktop\mybookmark1.csv 
--fields ["title","category","domain","url","notes","created_at","updated_at"]

database-tools: mongoimport

Import Into Local Machine

> mongoimport --db bookmark --collection bookmarks --type csv --file C:\Users\Admin\Desktop\mybookmark1.csv --headerline

MongoDB Tools

– **bsondump** – _display BSON files in a human-readable format_
– **mongoimport** – _Convert data from JSON, TSV or CSV and insert them into a collection_
– **mongoexport** – _Write an existing collection to CSV or JSON format_
– **mongodump/mongorestore** – _Dump MongoDB backups to disk in .BSON format, or restore them to a live database_
– **mongostat** – _Monitor live MongoDB servers, replica sets, or sharded clusters_
– **mongofiles** – _Read, write, delete, or update files in [GridFS](http://docs.mongodb.org/manual/core/gridfs/)_
– **mongotop** – _Monitor read/write activity on a mongo server_

Either make sure the first line of your data.csv file has field names of the data to be parsed and then execute:

mongoimport --db users --collection contacts --type csv --headerline --file data.csv

OR
Define the list of field names that the values of csv would be parsed in using –fields

mongoimport --db users --collection contacts --type csv --file data.csv --fields["name","surname","etc"]
> --headerline
> If using “--type csv” or “--type tsv,” use the first line as field names. Otherwise, mongoimport will import the first line as a distinct
> document.

Mongoimport of json file

mongoimport --db dbName --collection collectionName --file fileName.json --jsonArray

Reference

git clone
Migrate or Import Data into Your Cluster
Sample-starwars json

Leave a Reply

Your email address will not be published. Required fields are marked *