What's A Padding Factor?

  1. An update that doesn't change a document's underlying size can be done in-place, without moving the document.
  2. An update which does change a document's size forces MongoDB to move the document and update indexes, which is slower.
  3. MongoDB can add extra padding to new documents which can allow in-place updates even when the size changes.
  4. $inc an int doesn't usually increase a document's size since the field type usually remains the same.
  5. Using $push to add a new embedded document is good example of a document changing size.
  6. Analysis of past updates is used to determine what % of a document's size should be added as padding.
db.unicorns.insert({name: 'sporkles', vampires: 10})
db.unicorns.stats().paddingFactor
1

// $inc doesn't increase a document's size
db.unicorns.update({name: 'sporkles'}, {$inc: {vampires: 10}})
db.unicorns.stats().paddingFactor
1

// add a new field
db.unicorns.update({name: 'sporkles'}, {$set: {zombies: 2}})
db.unicorns.stats().paddingFactor
1.59

// compact added in 2.0, must repairDatabase in previous versions
db.unicorns.compact()
db.unicorns.stats().paddingFactor
1