Skip to content

Storing Data

Estimated time to read: 1 minute

How does MongoDB store data?

MongoDB uses documents to store data.

When documents are viewed or updated within the Mongo SHell, these are presented in JSON.

JSON

For a document to comply with the JSON format:

  • Documents must end with curly braces { }
  • Each key and value must be separated with a colon :
  • Each key:value pair must be separated with a comma ,
  • "keys" must be surrounded with quotation marks " "

Note

Keys are also knowns as fields in MongoDB.

{
    "_id" : "10021-2015-ENFO",
    "certificate_number" : 9278806,
    "result" : "No violation Issued",
    "address: { // This is a Sub-Document
        "city" : "TELFORD",
        "postcode" : "TF3 3AY",
        "number" : 4
    }
}

Storing Data within JSON has its pros and cons:

Pros Cons
User Friendly Text-Based
Human Readable Space-consuming
Familiar Supports limited datatypes

BSON

Binary JSON (BSON) aims to solve the issues when using JSON to store data.

BSON bridges the gap between binary representation and JSON format. It is optimised for speed, space and flexibility.

Since its creation, BSON has been extended to include optional non-json native data types, such as dates and binary data.

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

MongoDB stores data in BSON both internally and over the network.

More Info

For more information about JSON vs BSON and BSON, click their respective links.