Skip to main content

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

TypeBSON EquivalentDescriptionRecommendations for Use
StringStringUTF-8 text stringNames, addresses, textual content
NumberIntInt3232-bit integerCounters, flags, small ranges
NumberLongInt6464-bit integerTimestamps, large accumulators
NumberDoubleDoubleDouble precision floating pointStatistical calculations, approximate values
BooleanBooleanTrue or falseLogical states, enable/disable
DateDateDate and time in milliseconds since epochTime logging, TTL
NullNullAbsence of valueOptional fields, semantic consistency

2. Complex Types

TypeBSONDescriptionRecommendations for Use
ObjectDocumentNested object with subfieldsModeling hierarchical entities
ArrayArrayOrdered list of elements1:N relationships, value series
Embedded DocumentDocumentEmbedded subdocumentAvoid JOINs, improve read efficiency
ObjectIdObjectId12-byte hexadecimal unique IDPrimary keys, document references
BinaryBinaryBinary data (Buffer)Files, images, encoded tokens
CodeJavaScriptExecutable JS code snippetsAdvanced validations or filters (limited use)

3. Special and System Types

TypeBSONDescriptionRecommendations for Use
TimestampTimestampTimestamp with insertion orderSequential logs, internal replication
Decimal128Decimal128High precision decimal (128 bits)Finance, exact values
MinKeyMinKeyLowest possible valueLower limit comparisons
MaxKeyMaxKeyHighest possible valueUpper limit comparisons
UndefinedUndefinedUndefined 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.