Home 2. Importing, Exporting, and Querying Data
Post
Cancel

2. Importing, Exporting, and Querying Data

How Does MongoDB Store Data?

MongoDB uses documents to store data. When we view or update documents in the MongoDB shell, you’re working in JSON which stands for ‘JavaScript Standard Object Notation’. For a document to comply with the JSON format, you need to:

  • start and edn with curly brace {}
  • separate each key and value with a colon :
  • separate each key:value pair with a comma ,
  • "keys" must be surrounded by quotation marks ""

Note: Keys are also known as fields in MongoDB

For example:

1
2
3
4
5
6
7
8
9
10
11
12
{
    '_id': '1',
    'company_name': 'Fusemachines',
    'date': '2022-06-14',
    'position': 'Software Engineer Associate Trainee',
    'team': 'Data Engineering Team',
    'address': {
        'city': 'Kathmandu',
        'zip': 44600,
        'street': 'baneshword'
    }
}

The above example is a valid JSON.

Note: You may also have noticed the ‘address’ field which in itself contains a document as a value.

Pros and Cons of JSON

Pros of JSON

  • Friendly
  • Readbale
  • Familiar

Cons of JSON

  • JSON is a text-based format, and text parsing is very slow.
  • JSON’s redable format is far from space-efficient, another database concern.
  • JSON only supports a limited number of basic data types.

Therefore, MongoDB Decided to address these drawbacks. If you look at your data the way it is stored in memory inside MongoDB, then you’ll what is called BSON format-Binary JSON.

What is BSON?

BSON simply stands for “Binary JSON,” and that’s exactly what it was invented to be. BSON’s binary structure encodes type and length information, which allows it to be parsed much more quickly.

Since its initial formulation, BSON has been extended to add some optional non-JSON-native data types, like dates and binary data, without which MongoDB would have been missing some valuable support.

Languages that support any kind of complex mathematics typically have different sized integers (ints vs longs) or various levels of decimal precision (float, double, decimal128, etc.).

Not only is it helpful to be able to represent those distinctions in data stored in MongoDB, it also allows for comparisons and calculations to happen directly on data in ways that simplify consuming application code.

Why BSON?

In order to make MongoDB JSON-first, but still high-performance and general-purpose, the Binary JSON (BSON) was invented to bridge the gap between binary representation and JSON format. IT is optimized for speed, space and flexibility. The goal was to achieve high performance and general purpose focus.

Note: MongoDB stores data in BSON both internally and over the network. But that doesn’t mean you can’t think of MongoDB as a JSON Databse. Anything that can be natively stored in MongoDB and retrieved just as easily in JSON. BSON provides additional speed and flexibility, which is important to keep in mind when working with MongoDB.

Unlike systems that simply store JSON as string-encoded values, or binary-encoded blobs, MongoDB uses BSON to offer the industry’s most powerful indexing and querying features on top of the web’s most usable data format.

For example, MongoDB allows developers to query and manipulate objects by specific keys inside the JSON/BSON document, even in nested documents many layers deep into a record, and create high performance indexes on those same keys and values.

Importing and Exporting Data

We know, Data in MongoDB is stored in BSON format but viewed in JSON format. BSON is great but isn’t really human readable. If you’re just looking to store the data and then may be transfer it to a different system or cluster then your best bet would be to export in BSON. It’s lighter and faster. However, If I plan on viewing this data and reading through it locally after I export it, then a human redable JSON is a better choice.

Here,are four commands, two of which is used for importing and exporting data in JSON and two that is used for importing and exporting data in BSON.

JSONBSON
mongoimportmongorestore
mongoexportmongodump

Export

1
mongodump --uri "<Atlas Cluster URI>"

It allows to export the data in BSON format.

1
2
3
mongoexport --uri "<Atlas Cluster URI>"
            --collection=<collection name>
            --out=<filename>.json

It allows to export the data in JSON format.

Import

1
2
mongorestore --uri "<Atlas Cluster URI>"
            -- drop dump

It allows to import the data in BSON format.

1
2
mongoimport --uri "<Atlas Cluster URI>"
            --drop=<filename>.json

It allows to import the data in JSON format.

Querying in Mongo Shell

  • Use show dbs and show collections for viewing available namespaces
  • Use use <database_name> for connecting to a database
  • find() returns a cursor with documents that match the find query
  • count() returns the number of documents that match the find query
  • pretty() formats the documents in the cursor
This post is licensed under CC BY 4.0 by the author.