Data Types in NoSQL (MongoDB)
Technical document on data types in MongoDB, their internal structure, and recommended applications, with a practical focus for flexible modeling, efficient querying, and compatibility with distributed systems.
1. Primitive Types
| Type | BSON Equivalent | Description | Recommendations for Use |
|---|
String | String | UTF-8 text string | Names, addresses, textual content |
NumberInt | Int32 | 32-bit integer | Counters, flags, small ranges |
NumberLong | Int64 | 64-bit integer | Timestamps, large accumulators |
NumberDouble | Double | Double precision floating point | Statistical calculations, approximate values |
Boolean | Boolean | True or false | Logical states, enable/disable |
Date | Date | Date and time in milliseconds since epoch | Time logging, TTL |
Null | Null | Absence of value | Optional fields, semantic consistency |
2. Complex Types
| Type | BSON | Description | Recommendations for Use |
|---|
Object | Document | Nested object with subfields | Modeling hierarchical entities |
Array | Array | Ordered list of elements | 1:N relationships, value series |
Embedded Document | Document | Embedded subdocument | Avoid JOINs, improve read efficiency |
ObjectId | ObjectId | 12-byte hexadecimal unique ID | Primary keys, document references |
Binary | Binary | Binary data (Buffer) | Files, images, encoded tokens |
Code | JavaScript | Executable JS code snippets | Advanced validations or filters (limited use) |
3. Special and System Types
| Type | BSON | Description | Recommendations for Use |
|---|
Timestamp | Timestamp | Timestamp with insertion order | Sequential logs, internal replication |
Decimal128 | Decimal128 | High precision decimal (128 bits) | Finance, exact values |
MinKey | MinKey | Lowest possible value | Lower limit comparisons |
MaxKey | MaxKey | Highest possible value | Upper limit comparisons |
Undefined | Undefined | Undefined value (deprecated) | Not recommended, incompatible in new versions |
4. General Recommendations
- Flexible by design: NoSQL allows variable structure, ideal for heterogeneous or evolving data.
- Embedded documents: Improve read performance; avoid over-nesting that makes updates difficult.
- References vs. Embedding: Use references (
ObjectId) if data is shared across collections.
- Decimal128: Preferred in financial systems; avoid using
Double in monetary calculations.
- Array Indexing: MongoDB allows indexing individual elements of an array, useful for complex filtering.
- ⏱ TTL and dates: Combine
Date fields with TTL Index to automatically delete documents.
- Avoid deprecated types:
Undefined and Code are rarely used and have security and compatibility implications.