Lesson 2: Data Types in MongoDB / Learn

Press O for more options
5:57

Video Transcript (English)

Welcome back! Previously, you learned that MongoDB uses binary JSON, or BSON, to store data in documents.

BSON makes it easier to navigate, access, and manipulate data. It also supports many data types, which allows us to model a wider variety of use cases.

In this video, we’ll focus on the data types supported by BSON, and therefore MongoDB. These are: Booleans, Null values, String, Numbers, Object, Arrays, Dates, and ObjectIds.

Let’s begin!

We’ll start by showing a document from the sample_mflix movies collection. There’s a lot going on in here, but don’t worry - we’ll highlight the pieces you need to pay attention to as we go through the lesson.

For each data type we discuss, we will show a single example within the document. The exceptions are boolean and null values, which aren’t contained in this document. Those are pretty straightforward, though, so let’s start with them.

Boolean values have one of two possible values: true or false. They are used to represent binary states or conditions, such as whether a feature is enabled.

Null values are used to represent the absence of a value or an unknown value.

Next we have strings, which are sequences of characters used to represent text. In MongoDB, strings are enclosed in single or double quotes.

In our sample document, the 'title' field contains the string value 'A Fistful of Dollars'.

Keep in mind that there are other string values in this document - this is just one example.

Then there are numbers. In MongoDB, the number type can be integers or floating-point values.

Going back to our document, we see that the `runtime` field for example contains a numeric value of 99.

BSON extends numerical values beyond standard JSON. It supports two types of integers: 32-bit and 64-bit. In documents, these are represented as Int32 and Int64.

And when it comes to representing floating-point values, BSON includes data types called Double and Decimal128. These types are used for high-precision calculations and financial data.

Next up are objects. In JSON, objects are represented by curly braces and encapsulate related key-value pairs.

We use objects to embed documents within a parent document. This makes it easier to keep related data together as well as structure our documents.

In our sample document, the 'tomatoes' field contains an embedded document for its value, which is information from a movie rating site.

This embedded document contains more embedded documents for viewer, critic, and other fields.

Embedding documents provides great flexibility when modeling related data that should be accessed together. We’ll discuss embedded documents more in the data modeling lesson.

Next, arrays, denoted by square brackets, are used to store groups of values. Each element in an array can be any data type.

For example, in this document the `cast` field has an array because it contains multiple values.

The directors field is also an array even though in this case there’s a single value. However, we use an array for this field because a movie can have multiple directors.

Then there is the date data type. Dates in BSON are stored as a special type represented as a timestamp, which makes them efficient for querying and sorting. This format is known as the Unix epoch (milliseconds since January 1, 1970). It’s a standard in many systems due to its simplicity and precision.

In our movies document, the released field is a date field, represented with this timestamp format.

Date formats can be a challenge for developers, but the timestamp format keeps things simple and fast. It's efficient for storage, easy to convert to and from other date formats, and allows for fast comparisons and computations, which are essential for high-performance applications.

Now we’ll turn to a unique type in MongoDB, which is something called an ObjectId.

In MongoDB, every document must have a field called underscore ID with a unique value that serves as its unique identifier.

You can provide any value for the _id field.

MongoDB enforces uniqueness for the _id field. So, if you try to insert a document with a duplicate _id, MongoDB will reject it.

If you don’t provide a value for the _id field, MongoDB will automatically generate a unique ObjectID for the _id field.

In this example , the _id field is an ObjectId, which is a 24-character hexadecimal string.

The ObjectID consists of a timestamp, a random value, and an incrementing counter that guarantees its uniqueness.

And there you have it!

In this video, we explored BSON data types in MongoDB. These include: Booleans, Null values, String, Numbers, Objects, Arrays, Dates, and ObjectId.

If you want to dig further into BSON data types, you can check out the MongoDB documentation.

Understanding the different data types is crucial for accurately modeling our data to optimize MongoDB’s full potential. We’ll get to that later in this unit.

But first, let’s put all of this knowledge of the document model to use. In the next lesson, we’ll navigate our data in the Atlas UI, insert a document, and perform a basic query. See you there!