What are MongoDB data types?
Answer
MongoDB's BSON format supports more data types than JSON. String: UTF-8 encoded strings — most common type for text. Integer: Int32 (32-bit) and Int64 (64-bit) — numbers without decimal. Double: 64-bit floating-point — JSON "number" maps to this. Decimal128: high-precision 128-bit decimal for financial calculations (avoids floating-point errors). Use for money values. Boolean: true/false. Date: 64-bit integer representing milliseconds since Unix epoch. new Date() in mongo shell; ISODate("2024-01-15"). Always store dates as Date type, not strings. Timestamp: special BSON type for internal MongoDB use (not for application dates — use Date instead). ObjectId: 12-byte unique identifier, default _id type. Array: ordered list of values. Values can be any BSON type. Embedded document: nested document (object) as a value. Binary Data: byte arrays — for images, files (though large files should use GridFS or S3). Null: represents missing or undefined value. Regular Expression: stored as PCRE regex. JavaScript: JavaScript code (rare use). MinKey/MaxKey: special values that compare lower/higher than any other BSON value — used in shard key bounds. Symbol: deprecated. Type checking in queries: db.users.find({ age: { $type: "int" } }) or { $type: 16 } (BSON type number for Int32).
Previous
What is the difference between embed and reference in MongoDB schema design?
Next
What is the MongoDB query language and how does it differ from SQL?