TechAE Blogs - Explore now for new leading-edge technologies

TechAE Blogs - a global platform designed to promote the latest technologies like artificial intelligence, big data analytics, and blockchain.

Full width home advertisement

Post Page Advertisement [Top]

Import CSV file in MongoDB

Introduction:

MongoDB is an open-source cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses the BSON(Binary JSON) format to store and transfer documents. If you want to import a CSV file into MongoDB as a document, this tutorial will briefly explain how to do it.

Step 1: Download MongoDB

First of all, You have to download MongoDB Community Server. Then, you will download MongoDB Database Tools and place it in the MongoDB\Server\bin folder.

MongoDB Community Version is the open-source version of MongoDB and MongoDB Database Tools have a package of mongoimport which will help us import small or large files easily to your MongoDB database.

Step 2: Import CSV File


F:\MongoDB\Server\bin> mongoimport -d mydb --type csv --file path_of_filename.csv --headerline

Firstly, you have to open the command prompt in your bin folder, then write the above command. A database will be created with the name "mydb" and the collection name will be automatically set to your filename.

Step 3: Verification


F:\MongoDB\Server\bin> mongo
> use mydb
> show collections
> db.{collection_name}.find().pretty().limit(1)

The "mongo" command will open a command-line mongo terminal where you can verify and display your collections.

You can also verify your documents after opening MongoDB Compass which will be available to you after installing the MongoDB Community server edition.

There it is! You have finally imported the CSV file into MongoDB. Like this by just changing the --type parameter, you can import different file formats to MongoDB.

No comments:

Post a Comment

Bottom Ad [Post Page]