MongoDB Data Types Explained

 

When working with databases, understanding data types is essential. In MongoDB, data types define the kind of value that can be stored in a field. This helps MongoDB efficiently store, index, and query the data.

Unlike traditional relational databases, MongoDB uses a flexible, schema-less format based on BSON (Binary JSON). This allows it to support a variety of rich data types beyond simple strings and numbers.

Let’s explore the most commonly used data types in MongoDB, how they are stored, and when to use them.


๐Ÿ“ฆ What is BSON?

Before diving into types, it’s helpful to know that MongoDB stores data in BSON format — a binary version of JSON. BSON supports more data types than JSON (like Date, Decimal128, Binary, etc.), making it more powerful for database operations.


๐Ÿ—‚️ Common MongoDB Data Types

Here’s a breakdown of the most commonly used data types in MongoDB:


1. String ("some text")

  • Represents text data.

  • Most commonly used data type.

  • Stored as UTF-8 encoded.

Use When: Storing names, titles, addresses, etc.


2. Number (Int, Long, Double, Decimal128)

  • int32: 32-bit integer (NumberInt())

  • int64: 64-bit integer (NumberLong())

  • double: 64-bit floating point

  • Decimal128: High precision decimal, ideal for financial data

Use When: You need exact numeric values (e.g., age, price, quantity).


3. Boolean (true / false)

  • Stores a binary true/false value.

Use When: Flags like isActive, isAdmin, etc.


4. Date (ISODate())

  • Used to store date and time in UTC.

  • Supports operations like sorting, querying date ranges, etc.

Use When: Tracking timestamps, created/updated times.


5. ObjectId

  • A unique 12-byte identifier generated automatically for each document.

  • Used as the default _id field.

Use When: You want a unique key per document.


6. Array

  • Stores multiple values in a single field.

  • Can include mixed types (strings, numbers, objects, etc.)

Use When: Tags, lists, multiple addresses, etc.

json
"skills": ["MongoDB", "JavaScript", "Python"]

7. Embedded Document (Object)

  • A document inside another document.

  • Allows storing related data in a nested structure.

Use When: Modeling one-to-many or related data (like user and address).

json
"address": { "city": "Pune", "pin": 411001 }

8. Null

  • Represents a missing or null value.

Use When: Field is optional or value is unknown.


9. Binary Data

  • Used to store raw binary data such as images, files, or encrypted content.

Use When: Handling files or non-textual data in MongoDB.


10. Regular Expression (/pattern/)

  • Allows storing and querying text using patterns.

Use When: Searching for patterns in strings (e.g., email validation).


๐Ÿงช Example Document with Multiple Types

json
{ "_id": ObjectId("..."), "name": "Sharon", "age": 21, "isStudent": true, "scores": [88, 92, 79], "joined": ISODate("2023-10-01T00:00:00Z"), "address": { "city": "Pune", "state": "Maharashtra" }, "interests": null }

๐Ÿ›ก️ Best Practices

  • Choose the right type for the right data (e.g., use Boolean instead of "true" as a string).

  • Use Decimal128 for accurate currency calculations.

  • Index the fields based on type and query usage.

  • Avoid deeply nested or overly complex documents.


๐ŸŽฏ Final Thoughts

MongoDB's flexible data model lets you work with real-world data structures naturally. Mastering its data types helps you build more efficient, accurate, and scalable applications.

In the next blog, we'll explore "Working with ObjectId and _id in MongoDB", so stay tuned!


Prachi Deokar

University: Shree Balaji University, Pune

School: School of Computer Studies

Course: BCA (Bachelor of Computer Applications)

Interests: NoSQL, MongoDB, and related technologies

๐Ÿ“ธ Instagram ๐Ÿ”— LinkedIn ๐ŸŒ Official Website

Comments

Popular posts from this blog

MongoDB Master Guide

Covered Queries and Index Queries in MongoDB