Update Operators
In order to facilitate different types of update commands, MongoDB provides various update operators or update modifiers such as set, multiply, increment, and more. In the previous sections, we used the operator $set
, which is one of the update operators provided by MongoDB. In this section, we will learn some of the most commonly used operators and examples. Before we go through the operators, we will discuss their syntax. The following code snippet shows the basic syntax of an update expression that uses an update operator:
{ <update operator>: {<field1> : <value1>, ... } }
As per the preceding syntax, an operator can be assigned a document containing one or more pairs of field and value. The operator is then applied to each field using the respective value. An update expression like the previous one is useful when all the given fields need to be updated with the same operator. You may also want to update different fields of a...