Does mongodb store data in json?

Prerequisites

This tutorial assumes that you have installed and configured a MongoDB Atlas account and cluster. If you haven’t done so, here are a few useful steps:

  • Ready a cluster or create one. You can create one MongoDB Atlas cluster for free. Learn more about MongoDB Atlas here.
  • Set up a database along with a user and a password to import JSON documents.

How to import JSON into MongoDB

MongoDB uses BSON to store data. While JSON and BSON have slight differences such as availability of data types and readability, they can easily be converted into each other.

The process to import JSON into MongoDB depends on the operating system and the programming language you are using. However, the key to importing is to access the MongoDB database and parsing the file that you want to import. You can then go through each document sequentially and insert into MongoDB. You can also choose to bulk import the file into MongoDB. Let’s learn how we can import JSON documents into MongoDB now.

How to import JSON into MongoDB in Linux

To import JSON documents into MongoDB using Linux, open the terminal and install MongoDB tools. Run the following command from the terminal of your favorite Debian-based system, such as Ubuntu, to install MongoDB tools.

sudo apt install mongo-tools

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.

mongoimport --uri 
mongodb+srv://:@/ --collection  --type json --file 

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.

and are the username and password of the database user and refers to the cluster that holds the database.

and refer to the name of the database and the collection, into which you want to import the JSON file

Finally, 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.

mongoimport --uri 
mongodb+srv://:@/ --collection  --type json --file 

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.

...
    
        16
        16
    
    
        
            org.mongodb
            mongo-java-driver
            3.7.0-rc0
        
        
            commons-io
            commons-io
            2.10.0
        

        
            org.slf4j
            slf4j-nop
            2.0.0-alpha1
            test
        
        
            org.mongodb
            mongodb-driver-sync
            4.3.0-beta2
        

    
...

After you have ensured the dependencies exist, run the following code from your favorite code editor.


import java.io.*;
import com.mongodb.bulk.BulkWriteResult;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.BulkWriteOptions;
import com.mongodb.client.model.InsertOneModel;
import com.mongodb.client.result.InsertManyResult;
import com.mongodb.client.result.InsertOneResult;
import org.bson.Document;
import com.mongodb.client.MongoClients;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.mongodb.MongoWriteException;
import org.apache.commons.io.IOUtils;
import org.bson.json.JsonObject;
import javax.naming.StringRefAddr;
 
public class mongo {
    public static void main(String[] args) throws IOException {

        com.mongodb.client.MongoClient client = MongoClients.create( "");

        MongoDatabase database = client.getDatabase("");
        MongoCollection coll = database.getCollection("");

        try {

            //drop previous import
            coll.drop();

            //Bulk Approach:
            int count = 0;
            int batch = 100;
            List> docs = new ArrayList<>();

            try (BufferedReader br = new BufferedReader(new FileReader(""))) {
                String line;
                while ((line = br.readLine()) != null) {
                    docs.add(new InsertOneModel<>(Document.parse(line)));
                    count++;
                    if (count == batch) {
                        coll.bulkWrite(docs, new BulkWriteOptions().ordered(false));
                        docs.clear();
                        count = 0;
                    }
                }
            }

            if (count > 0) {
                BulkWriteResult bulkWriteResult=  coll.bulkWrite(docs, new BulkWriteOptions().ordered(false));
                System.out.println("Inserted" + bulkWriteResult);
            }

        } catch (MongoWriteException e) {
            System.out.println("Error");
        }

    }
}

Replace with the connection string to your MongoDB database, and with the name of the database and the collection, into which you want to import the JSON file and 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.

 pip3 install pymongo[srv]

Run the following code from a Python code editor to insert JSON into your MongoDB.

import pymongo
import json
from pymongo import MongoClient, InsertOne

client = pymongo.MongoClient()
db = client.
collection = db.
requesting = []

with open(r"") as f:
    for jsonObj in f:
        myDict = json.loads(jsonObj)
        requesting.append(InsertOne(myDict))

result = collection.bulk_write(requesting)
client.close()

Refer to the section above for more information on , , and .

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.

Ready to get started?

Import JSON data by creating a cluster free today.

Relevant FAQs

Does MongoDB use JSON?

How do I read a JSON file in MongoDB?

What format does MongoDB use for storing data?

MongoDB is a NoSQL Server in which data is stored in BSON (Binary JSON) documents and each document is essentially built on a key-value pair structure.

What is the difference between JSON and MongoDB?

BSON is just binary JSON (a superset of JSON with some more data types, most importantly binary byte array). It is a serialization format used in MongoDB. ... Difference Between JSON vs BSON..

Is JSON MongoDB a database type?

MongoDB and many other document-based NoSQL databases use JSON (JavaScript Object Notation) to represent data records as documents. There are many advantages to using JSON to store data. Some of them being: easiness to read, learn, and its familiarity among developers.

Why does MongoDB use BSON and JSON?

BSON is the binary encoding of JSON-like documents that MongoDB uses when storing documents in collections. It adds support for data types like Date and binary that aren't supported in JSON.