Beginner LevelMongoDB & Mongoose
What special BSON data types exist (Decimal128, Date, BinData, Regex)?
❓ Interview Prompt: Explain special BSON types: Decimal128 (exact financial calculations), Date (UTC timestamps), BinData (raw binary), and Regex.
BSON extends standard JSON with high-performance native data types:
•
Decimal128: 128-bit decimal floating point supporting 34 decimal digits of precision. Essential for financial transactions to avoid IEEE 754 float rounding errors!
•
Date: 64-bit integer representing UTC milliseconds since epoch.
•
BinData: Stores arbitrary raw binary byte arrays.
Code Implementation Example
typescript
import { Schema, Types } from 'mongoose';
const accountSchema = new Schema({
balance: {
type: Schema.Types.Decimal128, // Exact financial precision!
required: true,
get: (v: Types.Decimal128) => v ? v.toString() : '0.00'
}
});💡Using BSON Decimal128 for exact financial balance storage.
🎯 Interview Key Takeaway
Use BSON Decimal128 for storing currency and financial metrics to prevent IEEE 754 binary floating-point rounding errors.
#BSON Types#Decimal128#Date#Beginner
Back to all MongoDB & Mongoose Questions→