Learn how MongoDB uses JSON and BSON. Master document design, querying, and best practices for JSON-based NoSQL databases.
MongoDB stores data as BSON (Binary JSON), a binary representation of JSON-like documents. This format preserves JSON's flexibility while adding type information and efficient encoding. Understanding this relationship helps you design better document schemas.
JSON Characteristics:
BSON Characteristics:
A MongoDB document looks like JSON but with extended types:
{ "_id": ObjectId("6475f8e3b92a1a2b3c4d5e6f"), "name": "John Doe", "email": "[email protected]", "createdAt": ISODate("2026-06-01T10:00:00Z"), "isActive": true, "tags": ["developer", "mongodb"], "profile": { "age": 30, "location": "San Francisco" }, "balance": NumberDecimal("1234.56") }
Create: db.users.insertOne({ name: "Alice", email: "[email protected]", createdAt: new Date() });
Read: db.users.find({ age: { $gte: 18 } }); db.users.findOne({ email: "[email protected]" });
Update: db.users.updateOne( { _id: ObjectId("...") }, { $set: { name: "Alice Smith" } } );
Delete:
db.users.deleteOne({ _id: ObjectId("...") });
Store related data together:
{ "user": "john", "addresses": [ { "street": "123 Main St", "city": "NYC" }, { "street": "456 Oak Ave", "city": "LA" } ] }
Best for: One-to-few relationships, data accessed together
Store IDs and look up separately:
{ "user": "john", "orderIds": [ObjectId("..."), ObjectId("...")] }
Best for: One-to-many, unbounded arrays, large documents
Embed frequently accessed, reference rarely accessed:
{ "user": "john", "recentOrders": [{ /* embedded last 5 orders */ }], "allOrderIds": [ObjectId("...")] // reference for history }
Exact Match:
db.products.find({ category: "electronics" });
Nested Fields:
db.users.find({ "address.city": "San Francisco" });
Array Contains:
db.products.find({ tags: "featured" });
Multiple Conditions: db.products.find({ price: { $gte: 10, $lte: 100 }, inStock: true });
Aggregation Pipeline: db.orders.aggregate([ { $match: { status: "completed" } }, { $group: { _id: "$customerId", total: { $sum: "$amount" } }}, { $sort: { total: -1 } } ]);
Single Field:
db.users.createIndex({ email: 1 }); // ascending
Compound:
db.orders.createIndex({ userId: 1, createdAt: -1 });
Text Search:
db.articles.createIndex({ content: "text" });
Array:
db.products.createIndex({ tags: 1 }); // indexes each tag
1. Use _id Wisely
2. Keep Documents Under 16MB
3. Design for Your Queries
4. Handle Schema Evolution { "_schemaVersion": 2, "name": "John", // v2 fields "profile": { ... } }
Enforce structure at database level:
db.createCollection("users", { validator: { $jsonSchema: { bsonType: "object", required: ["name", "email"], properties: { name: { bsonType: "string" }, email: { bsonType: "string", pattern: "^.+@.+$" }, age: { bsonType: "int", minimum: 0 } } } } });
MongoDB's JSON-like documents offer flexibility unmatched by relational databases. By understanding BSON types, schema design patterns, and query optimization, you can build scalable applications that leverage MongoDB's strengths. Validate your document JSON structure with our formatter before importing to MongoDB.