---
# System prepended metadata

title: MongoDB

---

# MongoDB

## What is MongoDB

MongoDB is a document-oriented NoSQL database used for high volume data storage. Instead of using tables and rows as in the traditional relational databases, MongoDB makes use of collections and documents. Documents consist of **key-value** pairs which are the basic unit of data in MongoDB. Collections contain sets of documents and function which is the equivalent of relational database tables. 

MongoDB is a NoSQL document database. This means that data in MongoDB is stored as documents. 
These documents are in turn stored in what we call collections of documents. That's why MongoDB is categorized as a NoSQL document database.

### Document
- Document - a way to organize and store data as a set of field-value pairs.
- Field - a unique identifier for a datapoint.
- Value - data related to a given identifier.
    Example: 
    ```json
        {
            <field> :<value>,
            "name" : "walter white",
            "occupation" : "teacher",
            "major" : "chemistry"
        } // this is a document
    ```

### Collection
Collection is an organized store of documents in MongoDB, usually with common fields between documents. There can be many collections per database and many documents per collection.

![Database](https://i.imgur.com/RS5Vv6g.png)


### How are documents  represented in memory and what is the correct syntax for these objects that we call documents?

#### 1. What is JSON?

JavaScript objects are simple associative containers, wherein a string key is mapped to a value (which can be a number, string, function, or even another object). This simple language trait allowed JavaScript objects to be represented remarkably simply in text:

```json
{
  "_id": 1,
  "name" : { "first" : "John", "last" : "Backus" },
  "contribs" : [ "Fortran", "ALGOL", "Backus-Naur Form", "FP" ],
  "awards" : [
    {
      "award" : "W.W. McDowell Award",
      "year" : 1967,
      "by" : "IEEE Computer Society"
    }, {
      "award" : "Draper Prize",
      "year" : 1993,
      "by" : "National Academy of Engineering"
    }
  ]
}
```
As JavaScript became the **default** language of **client-side web development**, JSON began to take on a life of its own. By virtue of being both human- and machine-readable, and comparatively simple to implement support for in other languages, JSON quickly moved beyond the web page, and into software everywhere.

JSON shows up in many different cases:

- APIs
- Configuration files
- Log messages
- Database storage
- JSON quickly overtook XML, which is more difficult for a human to read, significantly more verbose, and less ideally suited to representing object structures used in modern programming languages.

#### 2. The MongoDB JSON Connection
MongoDB was designed from its inception to be the ultimate data platform for modern application development. JSON’s ubiquity made it the obvious choice for representing data structures in MongoDB’s innovative document data model.

However, there are several issues that make JSON less than ideal for usage inside of a database.

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

In order to make MongoDB JSON-first, but still high-performance and general-purpose, BSON was invented to bridge the gap: a binary representation to store data in JSON format, optimized for speed, space, and flexibility. It’s not dissimilar from other interchange formats like protocol buffers, or thrift, in terms of approach.

#### 4. 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**.
- BSON has been extended to **add** some **optional non-JSON-native data types**, like dates and binary data. Without that, MongoDB would have been missing some valuable support.

#### 5. MongoDB: JSON vs BSON

|  | JSON                                                                                                                                      | BSON                                                                                          |
| ------ | ----------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------- |
| Encoding    | UTF-8 String| Binary                    |
| Data Support  | String, Boolean, Number, Array| String, Boolean, Number (Integer, Float, Long, Decimal128...), Array, Date, Raw Binary
| Readability |Human and Machine | Machine Only

#### 6. Does MongoDB use BSON, or JSON?
MongoDB stores data in BSON format both internally, and over the network, but that doesn’t mean you can’t think of MongoDB as a JSON database. Anything you can represent in JSON can be natively stored in MongoDB, and **retrieved just as easily in JSON**.

Read more about BSON [here](https://www.mongodb.com/json-and-bson)
