home

What's A Padding Factor?

An update that doesn't change a document's underlying size can be done in-place, without moving the document.1
An update which does change a document's size forces MongoDB to move the document and update indexes, which is slower.2
MongoDB can add extra padding to new documents which can allow in-place updates even when the size changes.3
$inc an int doesn't usually increase a document's size since the field type usually remains the same.4
Using $push to add a new embedded document is good example of a document changing size.5
Analysis of past updates is used to determine what % of a document's size should be added as padding.6
  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
page 1 of 1