Skip to content
+91-7795206615
|
info@habilelabs.io

  • Home
  • About
  • Services
    • Custom Application Development
    • UI/UX Designing
    • Web Application Development
    • Offshore Product Services
    • Technical Outsourcing
    • ERP Services
  • Company
    • Careers
    • Case Studies
  • Specialization
    • Frontend Frameworks
      • Angular
      • ReactJS
      • VueJS
      • HTML / CSS
      • Javascript /Jquery
      • Bootstrap
      • Material design
    • Backend Frameworks
      • NodeJS
      • Meteor
      • GraphQL
      • Loopback
      • Salesforce
      • Spring Boot
      • Odoo
      • Laravel
    • Database / ORM
      • MySQL
      • PostgreSQL
      • Oracle
      • MongoDB
      • Google Firebase
      • Mongoose
      • Sequelize
      • Hibernate / JPA
    • Languages
      • Java Script
      • Dot Net
      • Java
      • Python
      • C / C++
      • PHP
      • AI / ML
      • Type Script
    • Mobile Frameworks
      • Ionic
      • Native Script
      • Native Android App
      • Native iOS App
      • Google Flutter
      • React Native
  • Blog
  • Hire Us

IMPORTING CSV FILES IN MONGODB DATABASE

Categories

  • Angular
  • Business Strategies
  • Cloud Services
  • CRM
  • Design Pattern
  • E-commerce
  • ERP Applications
  • Javascript
  • Meteor
  • Mobile development
  • Mongo DB
  • Node JS
  • Odoo
  • Our Partners
  • PHP
  • React
  • SAAS
  • Salesforce
  • SAP
  • Selenium
  • Tech stack Migration
  • Testing
  • UI-UX Design
  • Uncategorized
  • VAPT
  • Visualforce
  • Web Development
  • Web Security

Categories

  • Angular
  • Business Strategies
  • Cloud Services
  • CRM
  • Design Pattern
  • E-commerce
  • ERP Applications
  • Javascript
  • Meteor
  • Mobile development
  • Mongo DB
  • Node JS
  • Odoo
  • Our Partners
  • PHP
  • React
  • SAAS
  • Salesforce
  • SAP
  • Selenium
  • Tech stack Migration
  • Testing
  • UI-UX Design
  • Uncategorized
  • VAPT
  • Visualforce
  • Web Development
  • Web Security
Importing CSV Files in MongoDB Database

Importing CSV files in MongoDB is not as tough a task as it seems to be. With the advent of technological features in NoSQL, MongoDB also provides the features to import or export the files. MongoDB is one of the best NoSQL databases that has evolved in recent years. It provides the feature-full services to give the best results as expected by any developer or database expert.

Have you ever tried importing a CSV file in MongoDB? if not and you want it in your project to be done then you are in the right place. In this blog, I am going to tell you how to do so step by step.

But before that, let’s just check out what are CSV files?

What are CSV files?

CSV is the acronym for the comma-separated values file which allows saving the data in the table structured format. CSVs generally look like garden-variety spreadsheet data but they have an extension .csv. Generally, we save them in the text file format which is comma-separated-values. The same data which we see in CSV can be seen in a spreadsheet with rows and columns.

We can use CSV files with any spreadsheet program such as Microsoft Excel, Google spreadsheet, etc.

Importance of importing a CSV file

Import, see the word itself explains the importance. CSVs are the plain textual form of content that is kept inside a file. Let us just focus on why CSVs are important:

  1. Opened inside any text editor, unlike other file formats.
  2. Fairly flat and simple schema
  3. Programming languages can easily parse their data and generating them is easy.

So, importing CSV files in MongoDB also helps the developer to easily import the data by just reading the content inside it. Let’s just go through the steps to import a CSV file.

Importing a CSV file in MongoDB – steps and commands

Importing a CSV file in MongoDB is done through some commands which are followed step by step.

Follow these simple steps to import the CSV:

Step 1

Go to the directory where MongoDB is kept.

Step 2

Go to its Bin folder.

Step 3

Save or keep your CSV or TXT file here which you want to import. The image below shows exactly what you need to do.

mongodbCSV_file

This is our CSV file named word.csv.

The top word is the headerline which will be used in the command line MongoDB import command later.

Step 4

Now into the same folder open the cmd by pressing ctrl+L and typing cmd there. Press enter and you will see a cmd window now. (You can open this cmd window at this place by right-clicking the mouse along with shift key pressed – here you will see open command window here)

Step 5

Type the undermentioned command:

1
mongoimport -d wordCupDictionary -c dailywords --type CSV --file word.csv --headerline
importing csv files in mongo Db

And you will see that your document data will be imported.

Import the CSV

IMPORTANT NOTE:

wordCupDictionary is the database name

dailywords is the collection name.

–type CSV denotes that file type that is being imported is of type CSV.

word.csv is the file name that we are importing.

–headerline is the header of the CSV file, below which listed data is mentioned (here in this example it is ‘word ‘).

Following these steps, you will be successfully importing the CSV file inside the MongoDB database. This was the simple process inside MongoDB to achieve the target of MongoDB CSV file import.

MongoDB offers many other features that you may be interested in. For that you may like to read our blog.

Why choose MongoDB over other Databases?

Bottom Line:

In this blog cum tutorial, I have tried to give you a very brief step by step solution for importing a CSV file in MongoDB. In the same blog, I have tried explaining you the commands with the example.

Habilelabs is a one-stop solution for all mobile and web application development using MEAN, MERN, and full-stack technology. It is the official MongoDB ready partner and provides the development and consulting on MongoDB based solutions. Give us a call to find out more about our awesome services!

I hope this blog must have given you what you were looking for. In case you find yourself in some technical problem or have a query, then do let us know in the comments section below. Also, share your views about the blog that would really help me in coming up with some more knowledgeable content.

Posted byaishwaryaApril 3, 2017February 3, 2021Posted inMongo DBTags: Import csv file, Import CSV files in MongoDB, mongodb database

Post navigation


:

Join the Conversation

7 Comments

  1. Sugumar says:
    November 6, 2017 at 11:19 am

    Hi,
    While doing mongoimport –type csv we can provide –fieldfile as an option which has fields of json structure like emp.name, emp.0.place, address.1.city, etc..
    we can specify all this in one file one line per field.
    but when we do import the data shown is datatype mismatch number is replaced in strings.
    so how can we specify datatypes for the fields seperated with dot either in –fieldfile or –field option.

    Reply
    1. Ashu says:
      November 7, 2017 at 6:17 am

      Hello Sugumar,
      Mongoimport takes by default datatype based on your data but we can specify data type with the help of “–columnsHaveTypes”
      For an example :
      mongoimport -d wordCupDictionary -c dailywords –type CSV –fields col1.int32\(\),col2.double\(\),col3.string\(\) –columnsHaveTypes –file path/to/file.txt

      Regards
      Habilelabs team

      Reply
      1. Sugumar says:
        January 12, 2018 at 4:15 pm

        Thank u so much Ashu.

        Reply
  2. michal says:
    November 29, 2017 at 9:06 am

    Hii I am facing problem for to import csv file into mongodb database
    this error i am facing error validating settings: incompatible options: –file and positional argument(s)
    and this is the code
    mongoimport -d screenCaller -c screenCaller –type csv -file C:/xampp/htdocs/new lata-npa-nxx-state-city-zip.csv -headerline

    Reply
    1. Ashu says:
      November 30, 2017 at 4:56 am

      hello Michal,
      Seems like your systax is incorrect, -file should be –file(double hyphen without space) and same for –headerline(double hyphen without space)
      Correct syntax for mongoimport is following :

      --type csv

      mongoimport -d screenCaller -c screenCaller – -type csv – -file C:/xampp/htdocs/new lata-npa-nxx-state-city-zip.csv – -headerline
      Hope it will help.

      Regards
      Habilelabs Team

      Reply
  3. Prakriti Shrivastava says:
    April 19, 2018 at 10:51 am

    \bin>mongoimport -d db1 -c sales –type csv –file C:\Program Files\MongoDB\sales.csv –headerline
    2018-04-19T16:20:58.574+0530 error validating settings: incompatible options: –file and positional argument(s)
    2018-04-19T16:20:58.574+0530 try ‘mongoimport –help’ for more information

    Reply
    1. Ashu says:
      April 23, 2018 at 5:35 am

      Hi Prakriti,
      seems like your syntax is incorrect. someone asked same question so please look at above comment.
      if you still face any issue let us know.
      Thanks

      Reply
Leave a comment

Cancel reply

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

Leave a comment

Recent Posts

  • PyScript – Python for the Web Browser

    PyScript – Python for the Web Browser

  • Cross-Database with Association in Postgres with Sequelize

    Cross-Database with Association in Postgres with Sequelize

  • How can You Improve Your Communication Skills – Effective Tips

    How can You Improve Your Communication Skills – Effective Tips

  • 5 Reasons that Make CRM Software Beneficial for Every Business

    5 Reasons that Make CRM Software Beneficial for Every Business

  • Do Low-Code Solutions have a Future in Web Development?

    Do Low-Code Solutions have a Future in Web Development?

  • A Revenue Management Reset in Consumer Goods

    A Revenue Management Reset in Consumer Goods

Talk to our experts now

Have a project or looking for development team? Contact us.

Get a quote

About Us

Habilelabs Private Limited is the ISO 9001:2015 certified IT company, which has marked its flagship in 20+ countries with 100+ projects successfully

Company

  • About
  • Blog
  • Careers
  • Hire Us
  • Privacy Policy

Contact Us

  • +91-9828247415
  • +91-9887992695
  • info@habilelabs.io

Follow Us

Office

  • Habilelabs Private Limited
    4th Floor, I.G.M. School Campus,
    Sec-93 Agarwal Farm, Mansarovar,
    Jaipur, Rajasthan India
    Pin:302020