BSON Types

Understanding MongoDB's data types

๐Ÿ“ฆ What is BSON?

BSON (Binary JSON) is MongoDB's data format that extends JSON with additional data types like dates, binary data, and ObjectIds. It's designed for efficient storage and traversal of documents in MongoDB.


// BSON document example
{ _id: ObjectId("507f1f77bcf86cd799439011"), name: "John", age: 25 }
                                    

Common BSON Types

๐Ÿ”ข

Numbers

Integer and floating-point values

Int32 Int64 Double
๐Ÿ“

Strings

UTF-8 text data

String Symbol
๐Ÿ“…

Date & Time

Timestamp and date values

Date Timestamp
๐Ÿ†”

ObjectId

Unique document identifiers

ObjectId UUID

๐Ÿ”น String Type

Strings in MongoDB are UTF-8 encoded text. They're the most common data type for storing text information like names, descriptions, and messages.

// String examples
db.users.insertOne({
  name: "John Doe",
  email: "[email protected]",
  bio: "Software developer from New York",
  status: "active"
})

// Query strings
db.users.find({ name: "John Doe" })

// String with special characters
db.posts.insertOne({
  title: "Hello, World! ๐ŸŒ",
  content: "This is a test post with รฉmojis and spรซcial รงharacters"
})

Output:

{
  acknowledged: true,
  insertedId: ObjectId("507f1f77bcf86cd799439011")
}

๐Ÿ”น Number Types

MongoDB supports multiple numeric types for different precision and range requirements. Choose the appropriate type based on your data needs and performance considerations.

๐Ÿ”ธ Integer Types

// 32-bit integer (Int32)
db.products.insertOne({
  productId: NumberInt(12345),
  quantity: NumberInt(100),
  views: NumberInt(5000)
})

// 64-bit integer (Int64)
db.analytics.insertOne({
  userId: NumberLong("9876543210"),
  totalViews: NumberLong("1000000000"),
  timestamp: NumberLong("1640000000000")
})

๐Ÿ”ธ Floating-Point Numbers

// Double (64-bit floating point)
db.products.insertOne({
  name: "Laptop",
  price: 999.99,
  rating: 4.5,
  discount: 0.15
})

// Decimal128 (high precision)
db.financial.insertOne({
  accountId: "ACC123",
  balance: NumberDecimal("12345.67"),
  interestRate: NumberDecimal("0.0325")
})

๐Ÿ”น ObjectId Type

ObjectId is a 12-byte unique identifier automatically generated for the _id field. It contains timestamp, machine identifier, process ID, and counter, ensuring uniqueness across distributed systems.

// ObjectId structure: 4-byte timestamp + 5-byte random + 3-byte counter
// Automatic ObjectId generation
db.users.insertOne({
  name: "Alice"
  // _id is automatically generated
})

// Manual ObjectId creation
db.users.insertOne({
  _id: ObjectId(),
  name: "Bob"
})

// Create ObjectId from string
db.users.insertOne({
  _id: ObjectId("507f1f77bcf86cd799439011"),
  name: "Charlie"
})

// Get timestamp from ObjectId
var doc = db.users.findOne()
doc._id.getTimestamp()

// Query by ObjectId
db.users.findOne({ _id: ObjectId("507f1f77bcf86cd799439011") })

Output:

ISODate("2023-10-15T10:30:00.000Z")

๐Ÿ”น Date and Timestamp Types

Store temporal data with Date for general timestamps and Timestamp for internal MongoDB operations. Dates are stored as milliseconds since Unix epoch.

๐Ÿ”ธ Date Type

// Current date
db.events.insertOne({
  name: "Conference",
  createdAt: new Date(),
  eventDate: new Date("2024-12-25")
})

// Date from string
db.posts.insertOne({
  title: "My Post",
  publishedAt: new Date("2024-01-15T10:30:00Z")
})

// Date from milliseconds
db.logs.insertOne({
  message: "System started",
  timestamp: new Date(1640000000000)
})

// Query by date range
db.events.find({
  eventDate: {
    $gte: new Date("2024-01-01"),
    $lt: new Date("2024-12-31")
  }
})

๐Ÿ”ธ Timestamp Type

// Timestamp (internal use, replication)
db.replication.insertOne({
  operation: "insert",
  ts: new Timestamp(1640000000, 1)
})

// Get current timestamp
db.runCommand({ serverStatus: 1 }).oplog.timestamp

๐Ÿ”น Boolean Type

Store true/false values for flags, settings, and binary states:

// Boolean values
db.users.insertOne({
  name: "John",
  isActive: true,
  emailVerified: false,
  isPremium: true,
  notifications: {
    email: true,
    sms: false,
    push: true
  }
})

// Query by boolean
db.users.find({ isActive: true })
db.users.find({ emailVerified: false })

๐Ÿ”น Array Type

Store ordered lists of values, which can contain any BSON type including nested arrays and documents:

// Simple array
db.users.insertOne({
  name: "Alice",
  hobbies: ["reading", "gaming", "cooking"],
  scores: [85, 92, 78, 95]
})

// Array of objects
db.orders.insertOne({
  orderId: "ORD123",
  items: [
    { product: "Laptop", quantity: 1, price: 999.99 },
    { product: "Mouse", quantity: 2, price: 29.99 }
  ]
})

// Nested arrays
db.matrix.insertOne({
  name: "2D Array",
  data: [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
  ]
})

// Query array elements
db.users.find({ hobbies: "reading" })
db.users.find({ scores: { $gt: 90 } })

๐Ÿ”น Embedded Document Type

Store nested documents within documents for complex data structures:

// Embedded document
db.users.insertOne({
  name: "John Doe",
  address: {
    street: "123 Main St",
    city: "New York",
    state: "NY",
    zipCode: "10001",
    country: "USA"
  },
  contact: {
    email: "[email protected]",
    phone: "+1-555-0123"
  }
})

// Query embedded fields
db.users.find({ "address.city": "New York" })
db.users.find({ "contact.email": "[email protected]" })

// Update embedded fields
db.users.updateOne(
  { name: "John Doe" },
  { $set: { "address.zipCode": "10002" } }
)

๐Ÿ”น Binary Data Type

Store binary data like images, files, and encrypted content:

// Binary data
db.files.insertOne({
  filename: "profile.jpg",
  contentType: "image/jpeg",
  data: BinData(0, "iVBORw0KGgoAAAANSUhEUgAAAAUA...")
})

// Binary subtypes:
// 0 - Generic binary
// 1 - Function
// 2 - Binary (old)
// 3 - UUID (old)
// 4 - UUID
// 5 - MD5
// 128-255 - User defined

๐Ÿ”น Null and Undefined Types

Represent missing or undefined values in documents:

// Null value
db.users.insertOne({
  name: "John",
  middleName: null,
  nickname: null
})

// Query null values
db.users.find({ middleName: null })

// Check if field exists
db.users.find({ middleName: { $exists: false } })

// Undefined (deprecated, use null instead)
db.users.insertOne({
  name: "Jane",
  age: undefined  // Not recommended
})

๐Ÿ”น Regular Expression Type

Store and query using regular expressions for pattern matching:

// Store regex
db.patterns.insertOne({
  name: "Email Pattern",
  pattern: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
})

// Query with regex
db.users.find({ email: /^john/ })
db.users.find({ name: /doe$/i })

// Complex regex query
db.products.find({
  name: { $regex: "laptop", $options: "i" }
})

๐Ÿ”น Special Types

Additional BSON types for specific use cases:

MinKey and MaxKey

Special values for comparison operations

// MinKey - compares lower than all other values
db.test.insertOne({ value: MinKey() })

// MaxKey - compares higher than all other values
db.test.insertOne({ value: MaxKey() })

JavaScript Code

Store JavaScript functions

// Store JavaScript code
db.system.js.insertOne({
  _id: "myFunction",
  value: function(x) { return x * 2; }
})

๐Ÿ”น Type Checking

Query documents based on field types using the $type operator:

// Find by type number
db.collection.find({ age: { $type: "number" } })

// Find by type string
db.collection.find({ name: { $type: "string" } })

// Find by type array
db.collection.find({ tags: { $type: "array" } })

// Type numbers:
// 1 - Double
// 2 - String
// 3 - Object
// 4 - Array
// 5 - Binary data
// 7 - ObjectId
// 8 - Boolean
// 9 - Date
// 10 - Null
// 11 - Regular Expression
// 16 - 32-bit integer
// 18 - 64-bit integer

// Find by type number
db.collection.find({ age: { $type: 16 } })

๐Ÿง  Test Your Knowledge

What does BSON stand for?