MongoDB by default stores data in separate documents and collections, using references to relate data, which is a form of normalization. Embedding data (denormalization) is a design choice, not the default.
users and orders. Each order document stores a userId referencing a user. What will be the output of this aggregation query that looks up user info for each order?db.orders.aggregate([
{ $lookup: {
from: 'users',
localField: 'userId',
foreignField: '_id',
as: 'userInfo'
}},
{ $unwind: '$userInfo' },
{ $project: { _id: 0, orderId: 1, 'userInfo.name': 1 } }
])The $lookup stage joins orders with users by matching userId to _id. The $unwind stage converts the userInfo array to a single object. The $project stage selects only orderId and userInfo.name fields.
User document example: { _id: 1, name: 'John', address: { city: 'Oldtown', zip: '12345' } }
MongoDB uses dot notation in strings to update nested fields. Option B correctly uses 'address.city' as a string key. Options B, C, and D use invalid syntax.
Embedding related data reduces the need for multiple queries or joins, improving read performance. However, it can increase document size and complicate updates.
When data is denormalized by embedding, updates to the original source do not automatically propagate. The application must explicitly update all embedded copies to keep data consistent.