Consider a MongoDB collection users where each user document embeds an array of orders. Which query will generally be faster to retrieve all orders for a single user?
db.users.findOne({user_id: 123}, {orders: 1})Think about how embedding data reduces the need for multiple queries.
Embedding related data like orders inside a user document allows MongoDB to fetch all needed data in a single read, improving query speed.
What is a main reason to avoid very large documents in MongoDB?
Consider how document size affects performance and limits.
MongoDB has a document size limit (16MB), and very large documents can slow down operations and increase memory use.
Which MongoDB document design correctly references another collection?
User document referencing an Address document
Referencing uses ObjectId type fields to link documents.
Option D correctly uses an ObjectId field to reference another document. Option D embeds the address, not referencing. Option D uses ObjectId but inside an object, which is invalid. Option D uses a string instead of ObjectId.
Given a collection products where each document embeds an array tags, why does this query return no results?
db.products.find({tags: "electronics"})Think about how MongoDB matches array fields in queries.
MongoDB matches array fields by checking if the value is an element of the array. The query {tags: "electronics"} matches documents where tags array contains exactly "electronics" as an element, so it should work. But if the array contains objects or complex types, $in is safer. However, the main reason is that if the array contains objects, the query won't match a string. Using $in ensures matching any element in the array.
How does document design affect horizontal scaling in MongoDB sharded clusters?
Consider how document size and structure affect shard key distribution and performance.
Large embedded arrays can cause documents to be large and unevenly distributed, leading to hotspots on certain shards. Good document design helps distribute data evenly and improves scaling.