Skip to content

MongoDB - Data Types

MongoDB stores data in BSON (Binary JSON) format, which supports a wider range of data types than JSON. Understanding these types is crucial for effective data modeling. Here are the most important ones:

  • String: The most common data type. BSON strings are UTF-8. In general, drivers for each programming language will automatically convert from the language’s string format to UTF-8.
  • Double: A 64-bit floating-point number. This is the standard type for storing fractional values. Used for most numeric data unless arbitrary precision is needed.
  • Boolean: Stores a true or false value.
  • Object ID: The default type for the _id field. It’s a 12-byte unique identifier, generated using a timestamp, machine ID, process ID, and a counter. It’s designed to be unique across a distributed system.
  • Date: Stores the current date or time as a 64-bit integer representing milliseconds since the Unix epoch (Jan 1, 1970). It is timezone-aware. Always store times in UTC.
  • Array: Used to store lists or multiple values in a single key. Elements in an array can be of any BSON data type, including other arrays and documents.
  • Object: Used for embedded documents. This enables complex, nested data structures within a single parent document, which is a key feature of MongoDB’s document model.
  • 32-Bit Integer (Int32): Stores integer values. If you are running a 32-bit build of MongoDB, this will be the default integer type.
  • 64-Bit Integer (Int64): Stores larger integer values. Available in 64-bit builds of MongoDB. Drivers will typically handle the conversion from your language’s standard integer types.
  • Decimal128: A 128-bit decimal-based floating-point number, capable of emulating decimal rounding with exact precision. It’s essential for financial, scientific, and other calculations where precision is critical and floating-point rounding errors are unacceptable.
  • Null: Stores a null value or a missing field.
  • Binary Data: Used for storing binary data like images, audio files, or any non-textual data. It’s often better to use a dedicated file storage service like Amazon S3 and store the link in MongoDB, but this type is available for smaller binary blobs.
  • Regular Expression: Allows for storing regular expressions directly in BSON, which can be used in queries to perform advanced pattern matching on strings.
  • BSON Timestamp: A special 64-bit value used internally by MongoDB for replication and sharding. It is NOT for storing application-level date and time; use the Date type for that purpose.
  • Code: Can store a string of JavaScript code. Note: Executing database-side JavaScript (except for specific server-side functions) is generally discouraged in modern applications due to performance, complexity, and security concerns. Keep your logic in your application layer.
  • Symbol: (Deprecated) This type is similar to a string but is reserved for languages with a specific symbol type. It is largely deprecated and should be avoided in favor of strings.