Skip to main content
MongoDB
CHAPTER 05 Beginner

MongoDB Data Types | BSON, ObjectId, Arrays, Dates

Updated: May 16, 2026
15 min read

# CHAPTER 5

MongoDB Data Types Explained

1. Introduction

While MongoDB documents look exactly like standard JSON ({ "name": "John" }), underneath the hood, they are stored as BSON (Binary JSON). Standard JSON is very limited—it only understands strings, numbers, and booleans. It cannot mathematically differentiate between an integer (5) and a decimal (5.99), nor does it have a true "Date" format. BSON extends JSON, providing a rich, strongly-typed system that allows MongoDB to perform high-speed math and precise chronological sorting. In this chapter, we will explore the data types that power MongoDB.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Identify basic BSON data types (String, Number, Boolean).
  • Store complex lists using Arrays.
  • Store hierarchical data using Nested Objects (Embedded Documents).
  • Understand how MongoDB handles Dates.
  • Demystify the ObjectId Primary Key.

3. Basic Data Types

These are the standard types you will use every day, identical to JavaScript.
  • String: Used for text. Must be wrapped in quotes. {"name": "Alice"}
  • Number (Int32 / Double): By default, MongoDB treats numbers as 64-bit floating-point doubles. {"age": 28, "price": 9.99}
  • Boolean: Strictly true or false (no quotes!). {"isactive": true}
  • Null: Represents an explicitly missing or empty value. {"middlename": null}

4. Arrays (Lists of Data)

In SQL, storing a list of "Hobbies" requires creating a whole new table and joining it. In MongoDB, you simply use an Array. Arrays are enclosed in square brackets [].
json
12345
{
  "username": "gamer_guy",
  "hobbies": ["reading", "gaming", "hiking"],
  "high_scores": [1500, 2200, 950]
}

*(MongoDB is incredibly smart with Arrays. If you query "Find anyone who likes gaming", MongoDB automatically searches inside the array!)*

5. Objects (Embedded Documents)

If you have data that belongs together (like an Address), you don't keep the fields loose. You wrap them in curly braces {} to create an Embedded Document.
json
12345678
{
  "user": "Sarah",
  "shipping_address": {
    "street": "123 Main St",
    "city": "Austin",
    "zipcode": "73301"
  }
}

6. The Date Data Type

Standard JSON has no Date type; developers usually just save dates as text ("2023-10-01"), which destroys the database's ability to sort chronologically. BSON includes a native 64-bit Date type. In the Mongo Shell, you instantiate it using the new Date() JavaScript object.
javascript
1234
{
  "order_id": 99,
  "purchased_at": new Date() // Automatically captures the exact current time in UTC!
}

7. The Magic of ObjectId

In SQL, Primary Keys are usually Auto-Incrementing Integers (1, 2, 3). In MongoDB, the default Primary Key is called _id, and it uses a special 12-byte BSON type called an ObjectId.
json
1234
{
  "_id": ObjectId("64f1b2c890a82b1c45e6d78a"),
  "name": "Database Theory"
}

Why not use 1, 2, 3? MongoDB is designed for "Horizontal Scaling" (splitting your database across 50 different physical servers). If Server A and Server B both try to generate user #100 at the same millisecond, the database crashes. An ObjectId is a mathematically guaranteed globally unique string. It is generated using the server's MAC address, a process ID, and a timestamp. 50 servers can generate millions of ObjectIds per second and they will *never* collide!

8. Mini Project: The Ultimate BSON Document

Let's design a product document utilizing every data type we just learned.
javascript
12345678910111213
{
  "_id": ObjectId("507f1f77bcf86cd799439011"), // ObjectId
  "name": "Wireless Mouse",                    // String
  "price": 29.99,                              // Number
  "in_stock": true,                            // Boolean
  "tags": ["electronics", "accessories"],      // Array of Strings
  "manufacturer": {                            // Embedded Object
    "company": "TechCorp",
    "warranty_years": 2
  },
  "release_date": new Date("2023-01-15"),      // Date object
  "discount_code": null                        // Null
}

9. Common Mistakes

  • Confusing JSON Strings with Dates: Saving a date as "createdat": "2024-01-01" (a string) means you cannot run a query asking "Find all orders older than 30 days". Always use new Date().
  • Modifying the id: While MongoDB allows you to manually set the id to an integer ({"id": 5}), it is highly discouraged unless you have a very specific architectural reason. Rely on the default ObjectId.

10. Best Practices

  • Use Decimals for Money: For financial applications, standard floating-point numbers can cause microscopic rounding errors. MongoDB provides a NumberDecimal() type specifically for exact financial precision.
"price": NumberDecimal("19.99")

11. Exercises

  1. 1. What characters are used to denote an Array in MongoDB? What characters denote an Embedded Object?
  1. 2. Why does MongoDB use ObjectId instead of simple auto-incrementing integers (1, 2, 3) for Primary Keys?

12. MongoDB Challenges

Write a valid JSON/BSON document representing a Movie. It must include a string title, a number rating, a boolean indicating if it won an Oscar, and an array of strings representing the cast members.
json
123456
{
  "title": "Inception",
  "rating": 8.8,
  "won_oscar": true,
  "cast": ["Leonardo DiCaprio", "Joseph Gordon-Levitt", "Elliot Page"]
}

13. MCQ Quiz with Answers

Question 1

Under the hood, MongoDB does not store data as plain text JSON. What format does it use to optimize performance and support advanced data types like Dates?

Question 2

Which of the following data types is mathematically generated to guarantee absolute global uniqueness, allowing MongoDB to safely distribute data across hundreds of clustered servers?

14. Interview Questions

  • Q: Explain the mechanical difference between JSON and BSON. Give two examples of data types that BSON supports but standard JSON does not.
  • Q: A junior developer proposes changing all MongoDB id fields to simple integers (1, 2, 3) because "they look cleaner." Defend the architectural necessity of the 12-byte ObjectId in a distributed NoSQL environment.

15. FAQs

Q: Can I extract the creation time from an ObjectId? A: Yes! Because the first 4 bytes of an ObjectId represent the Unix timestamp of when it was created, you can actually extract the exact creation date/time of a document just by inspecting its ID, without needing a separate created
at field!

16. Summary

You are no longer writing plain text. By understanding the rich BSON type system, you can construct robust documents featuring sortable Dates, queryable Arrays, and deeply nested Objects, all uniquely identified by the distributed power of the ObjectId.

17. Next Chapter Recommendation

Our data structures are perfectly planned. Now, we must write them to the hard drive. In Chapter 6: Inserting Documents in MongoDB, we will open the shell and master the insertOne() and insertMany() operations.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·