After installing mongo-tools, you will have access to a series of CLI tools that can be used to interact with your MongoDB cluster. One of those tools is mongoimport, which can be used to import a JSON file into one of your collections. To do so, run the following command in your terminal.
You can get the connection string to your cluster from the Data Import and Export Tools section located under Command Line Tools in your MongoDB Atlas cluster.
<USERNAME> and <PASSWORD> are the username and password of the database user and refers to the cluster that holds the database.
<DATABASE> and <COLLECTION> refer to the name of the database and the collection, into which you want to import the JSON file
Finally, <FILENAME> is the full path and name of the JSON file you wish to import.
You can even import various other file formats such as TSV or CSV using mongoimport. Consult MongoDB’s official documentation on mongoimport for more information.
How to import JSON into MongoDB in Windows
To import a JSON document into MongoDB using Windows, download the MongoDB database tools. After the installation completes, you can use the mongoimport CLI tool to import JSON documents with the following command.
Refer to the section above for more information on the terminology used in this section.
Visit mongoimport for more information.
How to import JSON into MongoDB using Java
You can use a Java program, shown below, to import a JSON file into your MongoDB Atlas Cluster using Java and the MongoDB Java Driver. This program uses MongoDB Driver 4.3.0-beta 2. You can write this program on Intellij Idea and compile it using Java version 16.
Note: The code examples shown in this section require you to download maven dependencies. If your IDE doesn’t automatically download them for you, copy these dependencies into the pom.xml file.
After you have ensured the dependencies exist, run the following code from your favorite code editor.
Replace <CONNECTION STRING> with the connection string to your MongoDB database, <DATABASE> and <COLLECTION> with the name of the database and the collection, into which you want to import the JSON file and <FILENAME> with the full path and name of the JSON file.
In the above code, we read each line of the JSON file and insert one document at a time to an array list. This array list is then written into the database using the bulkWrite function. For more information on bulkWrite, you can visit the documentation page.
You can find the entire implementation including the json file on Github.
How to import JSON into MongoDB using Python
To import JSON into MongoDB using Python, install pymongo, the standard MongoDB driver library for Python, by running the following command in your terminal.
Run the following code from a Python code editor to insert JSON into your MongoDB.
Refer to the section above for more information on <CONNECTION STRING>, <DATABASE>, <COLLECTION> and <FILENAME>.
The above program loops through each document in the file and inserts it into a list. This list is then appended with the InsertOne function. After the loop reaches the end of the file, the program calls the bulk_write function to push all the documents to the collection at once.
Note: Because Python only escapes backslashes in a regular string, the name of the full path and name of the file to import is prepended with r in the code sample above. This way, that file becomes a raw string and Python doesn’t recognize the escape sequences.
You can find the entire implementation including the json file on Github.
Next steps
MongoDB makes it very easy to import JSON documents from different platforms and using different programming languages. Now that you’ve imported your JSON into MongoDB, why not explore your data using the aggregation framework, or visualize it using MongoDB Charts.