How to Import a JSON File Into MongoDB

Tahseen Tauseef Feb 02, 2024
  1. What Is MongoDB
  2. the mongoimport Command
  3. Import Data Into MongoDB Using the mongoimport Command
  4. Import JSON Files Into MongoDB Using the mongoimport Command
  5. Import CSV Files Into MongoDB Using the mongoimport Command
  6. Import TSV Files Into MongoDB Using the mongoimport Command
How to Import a JSON File Into MongoDB

This tutorial will discuss how to quickly and easily import all three file formats (JSON, CSV, and TSV) into your MongoDB database instance. So, without further ado, let’s get right into it.

What Is MongoDB

MongoDB is a NoSQL (non-relational) database system.

Fitting different data into a tight relational model is a pain if you work with enormous data. SQL databases, also known as Relational Database Management Systems (RDBMS), store data in rows and columns according to a pre-defined design that isn’t ideal for storing massive amounts of data.

Non-relational or NoSQL databases, such as MongoDB, have dynamic schemas developers may update on the fly. With an emphasis on greater scalability and quick searches, MongoDB has emerged as a preferable solution to SQL databases.

MongoDB’s dynamic schema architecture enables rapid application updates while simplifying programming for developers.

MongoDB is advantageous to developers since it offers official support for all common languages, including C, C++, C#, etc. In addition, net, Go, Java, Node.js, Perl, PHP, Python, Motor, Ruby, Scala, Swift, and Mongod are all examples of programming languages.

This enables developers to choose their preferred languages, resulting in shorter development times and fewer defects.

Key Features of MongoDB

  • Fast Queries: MongoDB queries are substantially quicker (up to 100 times faster) than queries in a typical Relational Database. This is because SQL databases store data in a normalized format, and queries for a single object or entity need joining data from numerous tables, resulting in slower processes.

  • Handle Large Unstructured Data: MongoDB’s document data architecture, which keeps all related data in a single document, allows it to handle enormous amounts of unstructured data seamlessly. MongoDB also permits you to query in a different way that is more workload-sensitive.

  • Horizontal Scaling: Horizontal scalability is one crucial area where most SQL databases fall short. They either support it ad hoc or solely on technologies still in their infancy.

    On the other hand, MongoDB allows for horizontal scaling, allowing you to add lower-cost commodity servers as needed.

  • Sharding: MongoDB allows you to store enormous amounts of data by distributing it over several servers linked to your app. When one server can’t manage the data’s size, the data is auto-sharded to another server.

  • Easy for Developers: MongoDB’s data structures may be mapped to those of computer languages. This will reduce the time and effort necessary for developers to learn new languages, configure MongoDB, and store data.

the mongoimport Command

Before executing the mongoimport command, all users must have the MongoDB database utilities installed on their machine.

The mongoimport command can import content from a mongoexport -created extended JSON, CSV, or TSV export. It also allows you to restore or import data from third-party export programs.

When it comes to administering your MongoDB database, this command is handy. It’s faster and multi-threaded than any bespoke script you might develop to perform your import.

Other MongoDB command-line tools, such as jq for JSON manipulation, csvkit for CSV manipulation, or even curl for dynamically obtaining data files from servers on the internet, can be paired with the mongoimport command.

The mongoimport command has the following syntax:

`mongoimport` <options> <connection-string> <file>

In the extended format, it looks like this:

mongoimport --host -u -p --authenticationDatabase --db  --colle --drop --file /name_of_file

In the following sections, we’ll go through the numerous ways you may use the syntax to import different file types such as JSON, CSV, and TSV.

Applications of the mongoimport Command

There may be several situations where you wish to reference some data or import a collection that already exists in your database, regardless of the sort of web application your team develops. These tasks include retrieving data from a collection of JSON or CSV files using the MongoDB mongoimport command.

MongoDB may be used as a backend source of information when your clients use your web applications, and mongoimport can be helpful.

When compared to file formats of CSV or TSV, JSON is recommended since it is both a hierarchical data format and is precise about the types of data it encodes, comparable to MongoDB documents. With that knowledge, we won’t encourage you to transform your data into JSON forms every time you wish to import it with mongoimport; instead, scrutinize it and determine whether or not you want to reorganize it.

Considerations in Using the mongoimport Command

Avoid using mongoimport and mongoexport for complete instance production backups as a precaution. The mongoimport and mongoexport commands cannot reliably retain all rich BSON data types since JSON only represents a subset of BSON’s types.

Therefore, instead of using mongodump and mongorestore, we propose using mongodump and mongorestore.

Here’s an example of the insert operation in the mongo shell that utilizes the shell mode presentation for the BSON types data date and data numberlong to demonstrate how the mongoexport and mongoimport commands employ strict mode representation to maintain information:

use test
db.traffic. insert( { _id: 1, volume: NumberLong ('2980001'), date: new Date() } )

The input supplied to data_numberlong must be quoted to avoid losing accuracy. Now, when you use mongoexport to export the data, we get the following:

mongoexport -- db test --collection traffic -- out traffic.json

To maintain type information, the exported data appears in a strict mode representation:

{ "_id" : 1, "volume" : { "$numberLong" : "2980001" }, "date" : { "$date" : "2014-03-13T13:47:42. 483-0400" } }

Import Data Into MongoDB Using the mongoimport Command

As mentioned earlier, the mongoimport can import JSON, CSV, or TSV files. We’ve divided the procedures for importing data from JSON, CSV, or TSV files into three parts for clarity and simple navigation.

Before moving on to these sections, make sure your MongoDB instance is connected to the mongoimport Windows, macOS, or Ubuntu application. While there are a few options for connecting mongoimport to your MongoDB database, we recommend using the –uri option, which looks like this:

mongoimport --uri 'mongodb+srv: // mycluster-ABCDE.azure.mongodb. net/test?retryWrites=true&w=majority'
   --username='USERNAME'
   --password='PASSWORD'
Note
If you’re using MongoDB Atlas Deployment, go to Database Deployments > Connect to retrieve your Atlas connection string.

You’ll have to create your own URI if you’re not utilizing Atlas Deployment. If you are connecting to one single server\, your URL will look like this- mongodb://your.server. host. name:port/, and if you run a replicaset\ and connecting to different hostnames your URI will look like this- mongodb://username:password @host1: port,host2:port/?replicaSet=replicasetname.

Import JSON Files Into MongoDB Using the mongoimport Command

To import JSON file(s) from one collection, use the code below:

mongoimport --db DB_Name --c collection_ Name --type= json --
file Name-of-the-file-to-import

Where,

  1. DB_Name represents the name of the database that contains the collection Collection_Name.
  2. type specifies the file type JSON (Optional field).
  3. The name and path of the JSON file to be imported/restored are represented by name-of-file-to-import.

You may alternatively write the above code more compactly as:

mongoimport -d DB_NAME - c COLLECTION _name --file Name-of-the-file-to-import

If you want to drop any existing collections with the same name as the one you’re trying to create/import, you may use the –drop flag with the mongoimport command.

mongoimport -d DB_NAME -c collect._name --drop --file Name-of-file-to-import

You may use the MongoDB mongoimport command to alter your host or port number when importing JSON data.

mongoimport --host 123. 123. 123.1 --port 4567 -d DB _ NAME -c collection _name --file Name-of-the-file-to-import

By default, mongoimport connects to a running mongo on localhost on port 27017.

Import CSV Files Into MongoDB Using the mongoimport Command

You may use the mongoimport command to import CSV files into a collection with the header line option. However, the header line argument instructs the mongoimport command not to import the first line as a document since it includes field names rather than data.

Use the following code to import a collection from a CSV file:

mongoimport --db DB_Name --collection collect._Name --type=csv --
headerline --file=Name-of-file-to-import

Where,

  1. DB_Name represents the name of the database that contains the collection Collection_Name.
  2. type specifies the file type CSV (Optional field).
  3. Headline details the mongoimport command to take the 1st record of CSV file(s) as field names.
  4. Name-of-file-to-import represents the name and path of the CSV file to be imported/restored

Import TSV Files Into MongoDB Using the mongoimport Command

When compared to CSV file formats, TSV files are fundamentally the same. Consequently, whether you use the mongoimport Windows application or another one, you may import TSV files using the same method as CSV files.

There is just one tiny difference: instead of using –type=csv, you may use the –type=tsv option to tell mongoimport about the new format.