Beginner LevelMongoDB & Mongoose
What is MongoDB and how does the BSON document data model work?
❓ Interview Prompt: Explain document-oriented databases, JSON vs BSON (Binary JSON), dynamic schema flexibility, and supported BSON data types.
MongoDB is a document-oriented NoSQL database that stores data in BSON (Binary JSON) format:
BSON Advantages over JSON:
1
Additional Data Types: Adds support for 'ObjectId', 'Date', 'Decimal128', 'BinData' (binary), and 'Regex'.
2
Fast Traversal & Parsing: Stores field length prefixes and element byte offsets, enabling fast scanning without parsing entire documents.
Code Implementation Example
typescript
import mongoose, { Schema, Document } from 'mongoose';
interface IUser extends Document {
name: string;
email: string;
metadata: Record<string, any>; // Flexible nested document
createdAt: Date;
}
const UserSchema = new Schema<IUser>({
name: { type: String, required: true },
email: { type: String, required: true, unique: true },
metadata: Schema.Types.Mixed,
createdAt: { type: Date, default: Date.now }
});💡Mongoose schema representing flexible BSON document properties.
🎯 Interview Key Takeaway
BSON extends JSON with rich binary data types and length prefixes, enabling fast server-side parsing and indexing.
#MongoDB#BSON#NoSQL#Beginner
Back to all MongoDB & Mongoose Questions→