Blog>
Snippets

Schema Design for Nested Documents

Explore schema design best practices that take advantage of MongoDB's document structure, particularly for nested objects and arrays, to optimize RAG data retrieval.
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<title>Schema Design Example</title>
<style>
  /* Placeholder for any needed CSS */
  body {
    font-family: Arial, sans-serif;
  }
</style>
</head>
<body>
<div id='schema-container'>
  <!-- Placeholder for schema information -->
</div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/mongodb/3.6.6/mongodb.min.js'></script>
<script>
  // Define a schema for a blog post with nested comments
  const blogPostSchema = {
    title: String,
    content: String,
    author: {
      name: String,
      email: String
    },
    comments: [{
      author: {
        name: String,
        email: String
      },
      content: String,
      date: Date
    }]
  };

  // Display the schema on the web page
  document.getElementById('schema-container').textContent = JSON.stringify(blogPostSchema, null, 2);
</script>
</body>
</html>
This HTML document includes a styled webpage that defines and displays a nested document schema for a blog post in MongoDB. The schema includes nested fields for author information and an array of comments, each with nested author details.
// MongoDB schema design best practice for nesting documents
const userSchema = new mongoose.Schema({
  name: String,
  email: String,
  address: {
    street: String,
    city: String,
    state: String,
    zip: String
  },
  // Embedding orders directly in the user document to optimize read performance
  orders: [{
    product: String,
    quantity: Number,
    date: Date
  }]
});

// The structure is optimized for retrieving all user data in a single query, reducing need for joins
This piece of code represents a MongoDB schema using Mongoose, designed for a User with nested documents. The address is nested within the user schema, and there is an array of orders (embedded subdocuments) to optimize read performance for cases where we typically need to retrieve all the user data together.