Explore Library
Code Quiz

Denormalized Updates in NoSQL

Spot the data-consistency bug that arises from denormalizing author data into MongoDB posts.

Codejavascript
// Schema decision: to make reads fast, each post EMBEDS a copy
// of the author's name (denormalized) alongside an authorId.
//   posts:  { _id, title, authorId, authorName }
//   users:  { _id, name, email }

async function updateUserName(db, userId, newName) {
  await db.collection('users').updateOne(
    { _id: userId },
    { $set: { name: newName } }
  );
  return 'updated';
}

// Later, the app reads post.authorName directly to render the byline.

Given this data model, what is the bug in updateUserName?