Because MongoDB stores data as BSON rather than plain JSON, it supports a richer set of data types than JSON has natively — this lesson covers the ones you will actually use.
| Type | Example | Notes |
|---|---|---|
| String | "Rahul Verma" | UTF-8 text, MongoDB's most common type |
| Number | 27, 3.14 | MongoDB distinguishes Int32, Int64, and Double internally |
| Boolean | true, false | |
| Date | ISODate("2026-08-19") | Stored as milliseconds since the Unix epoch |
| ObjectId | ObjectId("651f2a...") | The default type for _id — see below |
| Array | ["HTML", "CSS", "React"] | A field can hold a list of values, including other objects |
| Embedded Document | { city: "Kolkata" } | A nested object as a field's value |
| Null | null | Explicitly "no value," different from a missing field |
Always create dates with new Date() rather than storing a date as a plain string — a real Date type sorts and compares correctly, and can be queried with range operators like $gt:
db.orders.insertOne({
product: "Notebook",
orderedAt: new Date()
})An ObjectId is 12 bytes, built from a timestamp, a random value, and an incrementing counter — which means ObjectIds generated later sort after earlier ones, and you can extract an approximate creation time directly from one:
const id = ObjectId("651f2a9e1c4a2b0012a4f8d1")
id.getTimestamp()
// ISODate("2023-10-05T14:22:22.000Z")ObjectId encodes a creation timestamp
You rarely need to construct an ObjectId by hand — let MongoDB generate it on insert. Knowing it encodes a timestamp is mainly useful for understanding why ObjectIds are roughly sortable by creation order.