Skip to content

Common Mistakes

Unbounded Arrays and Huge Documents

The 16MB ceiling, arrays that never stop growing, and the split you should have made earlier.

IntermediateAbout 5 minutes

Problem. $push onto users.events or users.orderIds forever. The document hits 16MB, every profile read is huge, the multikey index explodes.

db.users.updateOne(
  { name: "Rahul" },
  { $push: { events: { type: "click", at: new Date() } } }
)
Bad
db.events.insertOne({
  userId: ObjectId("64a1b2c3d4e5f6a7b8c90001"),
  type: "click",
  at: new Date()
})

// optional cache on the user
{ name: "Rahul", recentOrderIds: [ /* last 10, capped */ ] }
Better — child collection, or a bounded subset

skills of length 2 and items of length 3 are not this bug. orderIds that grow with every checkout are. Projection $slice does not stop the write from growing the document. Bucket pattern if you are storing time series — still not an infinite array on Rahul.

Interview question

Why is an unbounded array inside a document a problem?

Think about it first.