Estuary

10 MongoDB Data Types: Complete Guide + Examples For 2024

Learn all about MongoDB data types and stay ahead of the competition with examples and practical tips for using each type effectively.

Share this article

Imagine you are creating an app that not only connects people across continents but also personalizes these connections based on real-time interests and preferences. But, a roadblock emerges – conventional data structures can't quite store various languages nor can they accommodate a wide array of user preferences. That's where MongoDB data types step up – your key to reshaping possibilities and embracing the future of your creation.

There is a long list of benefits that you get when you use MongoDB data types. If companies like Toyota Financial Services, Vodafone, and Cathay Pacific are using MongoDB for their data management needs, then there must be something remarkably exceptional about its capabilities and benefits.

This is exactly what we’ll explore in this guide. We’ll talk about the major data types that MongoDB has to offer and also see how you can effectively put them to use for your projects. 

Let’s start with a quick refresher. 

What Is MongoDB?

MongoDB Data Types - What Is MongoDB

MongoDB is an innovative, open-source database developed by MongoDB Inc. to meet the challenges of today's vast and complex data systems. It offers a unique approach to managing and storing data that differs from traditional databases.

MongoDB relies on flexible JSON-like documents rather than rigid tables to efficiently handle large volumes of data. This unique MongoDB structure not only provides adaptability but also simplifies data manipulation and retrieval. 

Key features of MongoDB include:

  • MongoDB’s cross-platform capabilities ensure that it operates seamlessly across different operating systems which makes it versatile for different application development.
  • It belongs to the NoSQL database category. This classification means that it doesn’t follow the conventional table-based relational database format, offering a more flexible approach to data management.
  • MongoDB is inherently document-oriented. This design choice means that instead of tables, data is stored in adaptable JSON-like documents. Each document can have its unique structure which makes it easy to tailor data storage to specific needs.
  • Collections in MongoDB serve as the primary data containers – similar to tables in traditional databases. However, these collections have a better advantage: they don't bind their documents to a fixed structure.
  • Documents, which are sets of key-value pairs, are the primary data units. Each document can vary in its fields and their design offers remarkable flexibility, given that there's no need for a predefined schema.

JSON vs BSON In MongoDB

MongoDB Data Types - JSON Vs BSON In MongoDB

Image Source

MongoDB uses the Binary JSON (BSON) format for data storage. To understand the data types in MongoDB, it’s important that you grasp the nuances of JSON and BSON.

JSON (JavaScript Object Notation) is a widely recognized data format. It is human-readable and represents data as a combination of key-value pairs. These pairs can include strings, numbers, objects, arrays, and more.

BSON (Binary JavaScript Object Notation), on the other hand, is MongoDB's binary representation of JSON-like documents. While it might look different because of its binary nature, it's designed to be more efficient for storage and retrieval purposes.

One of the major advantages of BSON is its ability to support additional data types that aren't present in standard JSON. This includes types like dates and binary data which are essential for many applications.

Now, why does MongoDB choose BSON over JSON?

The reason is performance. BSON's structure encodes type and length information which makes it faster to parse and generate than JSON. This speed is crucial for a database like MongoDB that needs to handle vast amounts of data quickly. Also, with BSON, MongoDB can handle more complex data types, going beyond the standard ones that JSON provides. 

While the Binary JSON documents of MongoDB can have different structures, this diversity in structure can cause challenges during data integration or analytics. SaaS tools like Estuary Flow come with advanced features that can maximize the potential of your MongoDB databases, transforming raw data into actionable insights with ease.

10 Core MongoDB Data Types: A Complete Overview

MongoDB Data Types - MongoDB Data Types Functionality

Image Source

MongoDB has many data types which can handle different requirements. These data types are not just a means to store data but they are tools that optimize querying to ensure data integrity and enhance storage efficiency.

In this tutorial, we’ll cover each data type in detail to get a clearer understanding of their uses and functionalities.

1. String

Strings are a fundamental data type in MongoDB and are used to represent textual data. They are necessary for storing names, descriptions, messages, or any form of text.

The string data type in MongoDB has specific features and behaviors:

  • UTF-8 encoding: MongoDB stores strings in UTF-8 format which supports most international characters. So if you're working with multilingual data or global applications, MongoDB handles it without a hitch.
  • Interactions with drivers: When you work with MongoDB through programming languages, the respective drivers manage the string's conversion to and from UTF-8. This conversion happens when you read from or write to the database.
  • Limitations: MongoDB has a limitation for BSON document size which is 16 megabytes. Since strings are part of these documents, the combined size of all fields including strings should not exceed this limit.

Example

Imagine that you want to store user profiles in your database. Here's how you could go about inserting a new user with a name and a short bio:

plaintext
db.users.insertOne({    "username": "Alice123",    "bio": "Software engineer with a passion for open-source projects." });

To retrieve this data, you'd use:

plaintext
db.users.find({"username": "Alice123"});

This will fetch Alice's profile, displaying both her username and bio.

2. Integer

MongoDB uses the integer data type to store whole numbers, both positive and negative. There are 2 main types of integers in MongoDB based on their size: 32-bit and 64-bit.

  • 32-bit Integer ("int"):
    • Ideal for everyday numbers.
    • Range: -2,147,483,648 to 2,147,483,647.
  • 64-bit Integer ("long"):
    • Designed for much larger numbers.
    • Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

Some important points to remember are:

  • Automatic type selection: When you insert an integer, MongoDB figures out if it should be a 32-bit or 64-bit based on the value.
  • Exceeding range: Going beyond the range for either type will give you an error. It's important to be mindful of the range, especially when handling large datasets.

Example

Here's how you insert an integer in MongoDB:

// Inserting a document with a 32-bit integer value for age

db.employees.insertOne({name: "Alice", age: 28});

// For larger numbers, like world population, MongoDB uses 64-bit integer

db.statistics.insertOne({population: 7500000000});

When you want to retrieve the inserted data:

plaintext
db.employees.find({name: "Alice"}); // Output: { _id: ObjectId("specific_id"), name: "Alice", age: 28 } db.statistics.find(); // Output: { _id: ObjectId("another_specific_id"), population: 7500000000 }

3. Double

The Double data type is used to store 64-bit IEEE 754 floating-point values in your MongoDB documents.

Some key aspects of this MongoDB data type are:

  • Size: It takes up 8 bytes, which aligns with the 64-bit IEEE 754 standard.
  • Precision: Double is great for handling a wide range of decimals but it doesn't promise 100% precision. So when working with decimals, sometimes the numbers might be just a bit off.
  • Limitations: This lack of exact precision is a trait of floating-point numbers. When your application depends on precise math, this can be a tricky point to navigate.

Example:

Let's see the Double data type in action. Here's how you would insert a student's details, including their test score, into a collection named "students".

plaintext
db.students.insertOne({    "student_name": "Liam Taylor",    "test_score": 88.7 })

After executing, MongoDB confirms with a response:

plaintext
{    "acknowledged": true,    "insertedId": ObjectId("6e4d3f2g9h1j5k6789l0m123") }

4. Boolean

The Boolean data type in MongoDB provides a straightforward way to represent true or false values. Originally it's a binary representation which makes it highly efficient in terms of storage.

When dealing with booleans in MongoDB, remember:

  • Storage efficiency: Compared to other data types, Booleans consume minimal space.
  • Limitations: Booleans are binary so they can only represent 2 states: true or false. If there's a need to represent a larger range of states, other data types or combinations would be needed.

Example

To see how Boolean datatype in MongoDB is used, consider a scenario where we want to store whether a product is in stock:

plaintext
db.products.insertOne({productName: "Laptop", isInStock: true})

When retrieving this product information, you can use:

plaintext
db.products.find({productName: "Laptop"}).pretty()

In this example, the isInStock field uses the Boolean datatype to indicate the stock status of the product.

5. Array

The array data type in MongoDB can store lists of simple data types like strings, numbers, or booleans. However, it is useful in storing lists of embedded documents, providing a multi-level depth to your data.

When querying arrays, MongoDB offers a set of operators to match array values precisely or to match arrays containing specific elements. You can also access or update specific elements within an array to make data manipulation efficient.

While arrays are powerful, they aren't without limitations:

  • Arrays that grow significantly over time might cause fragmented storage space. Regular maintenance like defragmentation can help.
  • MongoDB imposes a document size limit of 16MB. This means the total size of a document, including all its arrays and embedded documents, can't exceed this limit.
  • Overusing arrays can complicate query patterns. If every piece of data is stored in arrays, it can become challenging to fetch specific data points without complex queries.

Example

Suppose you're creating a database for a bookstore. Here's how you can structure a book document with an array to store authors:

plaintext
db.books.insert({   title: "The Great Novel",   authors: ["Jane Doe", "John Smith"],   genres: ["Fiction", "Mystery", "Thriller"],   ratings: [5, 4, 5, 4.5] });

Here, both authors and genres fields are string arrays while the ratings field is an array of numbers.

6. Null

As the name suggests, the null data type is a unique and straightforward method to represent the absence of a value.

When you store a field with a null value in MongoDB, it explicitly denotes the absence of a value for that field. Note that this is different from the field being completely missing from the document.

Both scenarios have their uses:

  • Explicit Null value: This is when a field exists in the document but its value is set to null. It shows that the value was intentionally set to null by the user or application.
plaintext
{  "product_code": "0000-XYZ",  "product_price": 39.99,  "product_color": null,  "product_availability": true }
  • Absent field: In this case, the field doesn't exist in the document at all. It's as if the field was never added.
plaintext
{  "product_code": "0000-XYZ",  "product_price": 39.99,  "product_availability": true }

There is a distinct advantage of using null in MongoDB – it provides clarity. When analyzing data, the null value tells you that the field was acknowledged but left empty or unset. On the other hand, a missing field might indicate that the data for that field was never collected or considered.

However, there are a few things about the null data type that you should consider:

  • Indexing: Fields with null values are indexable. When indexing a field that contains null, MongoDB treats null as a type and a value. 
  • Queries: When querying for a null value, MongoDB will return documents where the field is set to null or where the field is absent.
  • Limitations: While null is a powerful tool in MongoDB, over-relying on it can cause sparse data where the true intent of the data might become unclear over time. It's best to use null judiciously and with clear documentation.

7. Date

The Date data type is used for the exact representation of date and time values. It captures the number of milliseconds since the Unix epoch which began on January 1, 1970. With a 64-bit integer storage, it covers a vast range of about 290 million years – both into the past and the future.

A unique feature of the BSON Date type is that it's signed. If you come across negative values, they represent dates before the year 1970.

The date creation methods are:

  • Date(): Gives the current date but as a string.
  • new Date(): Outputs the current date as a date object wrapped in ISODate().
  • ISODate(): Like the above but can also be set to a specific date-time string.

Example

plaintext
// Insert a document with a date db.employees.insertOne({    "name": "Jane Smith",    "date_joined": ISODate("2022-08-22T12:45:42.389Z") });
plaintext
// Query and convert the date to a readable format var employee = db.employees.findOne({"name": "Jane Smith"}); print(new Date(employee.date_joined));

Here, we insert a record for an employee and their joining date. Then we fetch and convert that date to a more human-readable format using JavaScript's Date object.

8. Timestamp

The Timestamp data type functions differently from the regular Date type. It is a 64-bit value designed to represent a point in time and order of operations.

The structure of the Timestamp is:

  • The most significant 32 bits represent a time value that calculates the seconds elapsed since the Unix epoch.
  • The remaining 32 bits act as an incrementing ordinal to distinguish operations that occur within the same second.

One major use for this data type is tracking moments of document creation, editing, or updating. It helps maintain a precise chronology of events especially when multiple operations take place within a very short period.

Example

To insert a new Timestamp value in MongoDB, you should use the new Timestamp() function:

plaintext
db.mytestcoll.insertOne( {ts: new Timestamp()} );

9. Binary Data

In MongoDB, the Binary data type, or BinData, is designed for storing raw bytes that are ideal for non-textual data like images or audio files. Some key characteristics are:

  • Optimized for efficient querying.
  • Represents data not directly supported by JSON.

When you decide to store binary data in MongoDB, you have to use the BinData function. This function mainly takes 2 arguments:

  1. A type number that specifies the format of binary data. A commonly used type number is 1.
  2. A base64 encoded string that represents the binary content.

Example

To Insert binary data into a collection, use the BinData function:

plaintext
var imageData = BinData(1, "base64EncodedBinaryStringHere"); db.picturesCollection.insertOne({pictureData: imageData});

10. Object

MongoDB Data Types - Object

Image Source

You can use the Object data type in MongoDB when dealing with complex datasets as it provides a mechanism for storing nested documents. This nesting is achieved through embedded documents – documents nested within other documents.

Embedded documents group related data together inside a primary document. This structure keeps related information in one place to optimize data access.

However, there are some limitations for this MongoDB data type:

  • Depth: Nested documents cannot exceed 100 levels.
  • Size: The total size of a document, including its embedded ones, can't surpass 16MB.

Example

plaintext
// Defining a product with embedded document for dimensions db.products.insertOne({    "product_name": "Laptop",    "product_code": "A1B2C3",    "product_dimensions": {        "height": 15,        "width": 10,        "depth": 0.5    } });
plaintext
// Retrieving the product db.products.find({"product_code": "A1B2C3"}).pretty();

In this example, we embedded the product dimensions directly within the main product document to show the use of the Object data type for embedded documents.

Understanding Data Type Checking & Conversion In MongoDB

MongoDB provides powerful tools for both checking and converting data types.

This section provides insights into the methods and practices for type checking and conversion in MongoDB.

Checking Data Types

MongoDB has an easy method to check the data type of a field – the $type operator. With this operator, you can query documents based on the BSON type of a specific field.

For instance, if you want to find documents where a field is of string type, you will use:

plaintext
{ field: { $type: "string" } }

If you are interested in documents that have a field with multiple possible types, you can pass an array of BSON types to the $type operator:

plaintext
{ field: { $type: ["string", "int"] }

Converting Data Types

Sometimes, data isn't stored in the format that is needed during processing. MongoDB has many aggregation operators to convert fields from one type to another.

  • $toInt: This function converts a value to an integer. For example, if you want to change a string field named "price" to an integer, you'd use:
plaintext
db.collection.aggregate([{ $addFields: { price: { $toInt: "$price" } } }])
  • $toDouble: This operator will change a value to a double.

For a field "weight" that needs conversion to double, the command would be:

plaintext
db.collection.aggregate([{ $addFields: { weight: { $toDouble: "$weight" } } }])
  • $toString: For converting a non-string value to a string, use $toString.

For example, if "date" is a field with date type and you need it as a string:

plaintext
db.collection.aggregate([{ $addFields: { date: { $toString: "$date" } } }])
  • $toDate: If you have date values stored as strings and wish to convert them to the date type, use $toDate.

For a string field "birthday" should to be a date:

plaintext
db.collection.aggregate([{ $addFields: { birthday: { $toDate: "$birthday" } } }])
  • $convert: The $convert operator can change a field's data type to any other type. For instance, to convert the "age" string field to an integer:
plaintext
db.collection.aggregate([  {    $project: {      age: {        $convert: {          input: "$age",          to: "int"        }}}}])

Note that if a conversion is not possible, the field will be set to null.

Estuary Flow and MongoDB For Enhanced Data Management

MongoDB Data Types - Estuary Flow

Estuary Flow is our cutting-edge platform designed for real-time data processing. When it comes to MongoDB, Flow’s NoSQL database, known for its flexibility with dynamic schemas, has many advantages. 

Let’s look at how Flow can improve your experience with MongoDB databases:

Schema Inference

Flow can transform the unstructured data from MongoDB into structured data using its advanced schema inference capabilities. This transformation simplifies data processing, helping you extract meaningful insights directly from your MongoDB collections.

Real-Time Materializations

With Flow, you can maintain real-time views of your MongoDB data across different systems. This way, you get instant access to updated information for a comprehensive view of your MongoDB data.

Data Transformations

Flow transforms data using streaming SQL and JavaScript. This gives a flexible way to manipulate and customize your MongoDB data streams to fit specific needs.

Change Data Capture (CDC)

Flow's CDC functionality integrates seamlessly with MongoDB. It captures and tracks changes in your MongoDB data in real time and ensures that your downstream systems are always updated with the most recent changes.

MongoDB Connectors

Flow offers both source and destination connectors for MongoDB. The source connector captures data from MongoDB collections while the destination connector helps materialize data back into MongoDB. This two-way connectivity streamlines data movement and integration.

Conclusion

The wide range of MongoDB data types lets you shape databases with unmatched flexibility and efficiency. These data types can easily handle evolving application needs while optimizing storage and query performance.

However, as datasets grow larger and more complex, working with MongoDB becomes challenging. This is where data integration tools like Estuary Flow can help you. With capabilities to effortlessly integrate MongoDB databases and transform dynamic document schemas into structured data, Flow simplifies deriving value from MongoDB.

If you are looking to enhance your MongoDB application development and data management, give Estuary Flow a try. You can sign up for free and start integrating your MongoDB databases in real time.

Start streaming your data for free

Build a Pipeline
Share this article

Table of Contents

Build a Pipeline

Start streaming your data for free

Build a Pipeline

About the author

Picture of Jeffrey Richman
Jeffrey Richman

With over 15 years in data engineering, a seasoned expert in driving growth for early-stage data companies, focusing on strategies that attract customers and users. Extensive writing provides insights to help companies scale efficiently and effectively in an evolving data landscape.

Popular Articles

Streaming Pipelines.
Simple to Deploy.
Simply Priced.
$0.50/GB of data moved + $.14/connector/hour;
50% less than competing ETL/ELT solutions;
<100ms latency on streaming sinks/sources.