Given a collection users where each user document embeds a single profile document, what will be the output of the following query?
{ _id: 1, name: "Alice", profile: { age: 30, city: "NY" } }Query:
db.users.findOne({ _id: 1 }, { "profile.city": 1, _id: 0 })db.users.findOne({ _id: 1 }, { "profile.city": 1, _id: 0 })Projection returns the embedded document fields as nested objects.
The projection { "profile.city": 1, _id: 0 } returns the profile field with only the city subfield included. The result nests city inside profile.
Which of the following MongoDB insert statements correctly embeds a one-to-one address document inside a customer document?
Embedding means nesting the document inside the main document in one insert.
Option D correctly nests the address document inside the customer document in a single insert. Options B and D use incorrect syntax for insertOne. Option D uses an array which is not one-to-one embedding.
You have a products collection where each product embeds a specs document. You want to frequently query products by a field inside specs, for example specs.weight. What is the best way to optimize these queries?
Indexes speed up queries on specific fields.
Creating an index on specs.weight allows MongoDB to quickly find products by that field inside the embedded document. Text indexes are for string search, splitting collections adds complexity, and filtering after fetching all data is inefficient.
You have documents with embedded profile in users. You run this query:
db.users.find({}, { name: 1 })But the profile field is missing in the results. Why?
db.users.find({}, { name: 1 })Projection controls which fields appear in results.
When projection specifies only name: 1, MongoDB returns only the name field and excludes all others by default, including profile. To include profile, it must be explicitly projected.
In MongoDB, when designing a schema for a one-to-one relationship, why might embedding the related document be better than using references?
Think about how data is stored and accessed in MongoDB.
Embedding stores related data in the same document, so queries can retrieve all data in one read without joins. MongoDB documents have size limits, no foreign key constraints, and indexes must be created explicitly.