MongoDB Update Operators
Modifying documents with operators
$set
db.users.updateOne(
{ name: 'John' },
{ $set: { age: 26, city: 'NYC' } }
); # set field values
$unset
db.users.updateOne(
{ name: 'John' },
{ $unset: { age: '' } }
); # remove field
$inc
db.products.updateOne(
{ _id: 1 },
{ $inc: { views: 1 } }
); # increment by 1
$mul
db.products.updateOne(
{ _id: 1 },
{ $mul: { price: 1.1 } }
); # multiply by 1.1
$rename
db.users.updateMany(
{},
{ $rename: { 'name': 'fullName' } }
); # rename field
Array: $push
db.users.updateOne(
{ name: 'John' },
{ $push: { tags: 'mongodb' } }
); # add to array
Array: $pull
db.users.updateOne(
{ name: 'John' },
{ $pull: { tags: 'old-tag' } }
); # remove from array
Array: $addToSet
db.users.updateOne(
{ name: 'John' },
{ $addToSet: { tags: 'mongodb' } }
); # add if not exists