0
0
MongoDBquery~30 mins

Document size and growth patterns in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Document Size and Growth Patterns in MongoDB
📖 Scenario: You are managing a MongoDB collection that stores user profiles for a social media app. Each user document contains basic information and an array of posts. You want to understand how document size grows as users add more posts.
🎯 Goal: Build a MongoDB document structure for user profiles, add a configuration for maximum posts allowed, insert posts dynamically, and finally update the document to reflect the growth pattern.
📋 What You'll Learn
Create a MongoDB document for a user with specific fields
Add a configuration variable for maximum posts allowed
Insert posts into the user's posts array using an update operation
Update the document to reflect the final state with all posts
💡 Why This Matters
🌍 Real World
Social media apps and content management systems often store user-generated content in documents that grow over time. Understanding document size helps optimize performance and storage.
💼 Career
Database administrators and backend developers need to manage document growth to maintain efficient queries and avoid exceeding size limits in MongoDB.
Progress0 / 4 steps
1
Create the initial user document
Create a MongoDB document called userProfile with these exact fields: _id set to 1, name set to "Alice", and an empty array posts.
MongoDB
Need a hint?

Use a JavaScript object with keys _id, name, and posts initialized as an empty array.

2
Add a maximum posts configuration
Create a variable called maxPosts and set it to 3 to limit the number of posts a user can have.
MongoDB
Need a hint?

Just create a simple variable maxPosts and assign the number 3.

3
Add posts to the user document
Use a for loop with variable i from 1 to maxPosts inclusive to push new post objects into userProfile.posts. Each post object should have postId set to i and content set to `Post number ${i}` using template strings.
MongoDB
Need a hint?

Use a for loop from 1 to maxPosts and push objects with postId and content into the posts array.

4
Update the user document to reflect growth
Add a new field postCount to userProfile and set it to the length of the posts array using userProfile.posts.length.
MongoDB
Need a hint?

Assign userProfile.postCount to the length of the posts array.