In MongoDB, when designing a schema for a read-heavy application, why is embedding related data inside a single document often preferred over referencing separate documents?
Think about how many database calls are needed to get all related data.
Embedding related data in one document means MongoDB can retrieve all needed information in a single read operation, which is faster for read-heavy workloads.
Given a collection users where each user document embeds an array of orders, what will be the output of this query?
db.users.find({"orders.status": "shipped"}, {name: 1, orders: 1})Assuming the collection has:
{ "name": "Alice", "orders": [{"id": 1, "status": "shipped"}, {"id": 2, "status": "pending"}] }
{ "name": "Bob", "orders": [{"id": 3, "status": "pending"}] }Remember that MongoDB returns the whole document if any embedded document matches the query.
The query matches users with any order having status 'shipped'. It returns the full user document with all orders, not just the matching orders.
Which MongoDB schema design is best for a read-heavy workload where each user has thousands of comments, and you want to quickly fetch the latest 10 comments?
Think about document size limits and efficient querying.
Embedding thousands of comments in one document risks exceeding size limits and slows reads. Storing comments separately with proper indexes allows fast queries for latest comments.
You have a collection products with fields category, price, and rating. Your app frequently queries products by category and sorts by price ascending. Which index will optimize these queries best?
Consider the order of fields in compound indexes for filtering and sorting.
A compound index on category then price supports filtering by category and sorting by price efficiently.
Consider this query on a large orders collection:
db.orders.find({ status: "completed" }).sort({ orderDate: -1 }).limit(5)The query is slow despite indexes on status and orderDate separately. What is the main reason?
Think about how MongoDB uses indexes for filtering and sorting together.
MongoDB can only use one index efficiently per query stage. Separate indexes on status and orderDate don't help sorting after filtering. A compound index on {status: 1, orderDate: -1} is needed.