0
0
MongodbHow-ToBeginner · 4 min read

How to Design Schema in MongoDB: Best Practices and Examples

To design a schema in MongoDB, define the structure of your documents using collections and embed related data or use references depending on your access patterns. Use Mongoose or similar ODM tools to enforce schema rules in your application.
📐

Syntax

In MongoDB, schema design involves defining the shape of documents inside collections. You can embed documents or reference other documents. Using Mongoose in Node.js helps define schemas with types and validation.

  • Collection: A group of documents.
  • Document: A JSON-like object stored in a collection.
  • Embedded Document: Nested object inside a document.
  • Reference: Storing the ID of another document.
javascript
const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
  name: String,
  email: String,
  address: {
    street: String,
    city: String
  },
  orders: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Order' }]
});

const User = mongoose.model('User', userSchema);
💻

Example

This example shows a User schema with embedded address and referenced orders. It demonstrates how to structure related data efficiently.

javascript
const mongoose = require('mongoose');

// Order schema
const orderSchema = new mongoose.Schema({
  product: String,
  quantity: Number,
  price: Number
});
const Order = mongoose.model('Order', orderSchema);

// User schema with embedded address and referenced orders
const userSchema = new mongoose.Schema({
  name: String,
  email: String,
  address: {
    street: String,
    city: String
  },
  orders: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Order' }]
});
const User = mongoose.model('User', userSchema);

async function run() {
  await mongoose.connect('mongodb://localhost:27017/testdb');

  const order1 = new Order({ product: 'Book', quantity: 2, price: 20 });
  await order1.save();

  const user = new User({
    name: 'Alice',
    email: 'alice@example.com',
    address: { street: '123 Main St', city: 'Wonderland' },
    orders: [order1._id]
  });
  await user.save();

  const foundUser = await User.findOne({ name: 'Alice' }).populate('orders');
  console.log(foundUser);
  await mongoose.disconnect();
}

run();
Output
{ _id: ObjectId("..."), name: 'Alice', email: 'alice@example.com', address: { street: '123 Main St', city: 'Wonderland' }, orders: [ { _id: ObjectId("..."), product: 'Book', quantity: 2, price: 20 } ], __v: 0 }
⚠️

Common Pitfalls

Common mistakes in MongoDB schema design include:

  • Embedding too much data causing large documents and slow queries.
  • Overusing references leading to many joins and slower reads.
  • Not considering access patterns before choosing embedding or referencing.
  • Ignoring schema validation which can cause inconsistent data.

Balance embedding and referencing based on how your app reads and writes data.

javascript
/* Wrong: Embedding large arrays that grow indefinitely */
const wrongSchema = new mongoose.Schema({
  name: String,
  comments: [{ text: String, date: Date }]
});

/* Right: Use referencing for large or growing data */
const commentSchema = new mongoose.Schema({
  userId: mongoose.Schema.Types.ObjectId,
  text: String,
  date: Date
});
const Comment = mongoose.model('Comment', commentSchema);

const postSchema = new mongoose.Schema({
  title: String,
  commentIds: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }]
});
📊

Quick Reference

ConceptDescriptionWhen to Use
Embedded DocumentStore related data inside a documentWhen data is accessed together and size is small
ReferenceStore ObjectId linking to another documentWhen data grows large or is shared across documents
Schema ValidationEnforce data types and rulesAlways, to keep data consistent
DenormalizationDuplicate data for faster readsWhen read speed is critical and updates are rare

Key Takeaways

Design schema based on how your application reads and writes data.
Use embedded documents for related data accessed together and small size.
Use references for large or shared data to avoid large documents.
Enforce schema validation to keep data consistent and reliable.
Balance embedding and referencing to optimize performance and scalability.