Skip to content

Adding, Removing and Renaming the fields

Renaming Fields

  • Rename the fields from isFeatured to isFeature whose price is 123.
shop> db.products.updateMany({price:123}, {$rename: {'isFeatured': 'isFeature'}})
{
  acknowledged: true,
  insertedId: null,
  matchedCount: 7,
  modifiedCount: 7,
  upsertedCount: 0
}

Removing fields

shop> db.products.updateMany({isFeature:false}, {$unset: {'isFeature': 1}})
{
  acknowledged: true,
  insertedId: null,
  matchedCount: 7,
  modifiedCount: 7,
  upsertedCount: 0
}

Adding New Fields

shop> db.comments.updateOne({_id:5}, {$set: {'user':'Jessy'}})
{
  acknowledged: true,
  insertedId: null,
  matchedCount: 1,
  modifiedCount: 1,
  upsertedCount: 0
}