- Reference >
- Operators >
- Update Operators >
- Array Update Operators >
- $each
$each¶
On this page
Definition¶
-
$each¶ The
$eachmodifier is available for use with the$addToSetoperator and the$pushoperator.Use with the
$addToSetoperator to add multiple values to an array<field>if the values do not exist in the<field>.Use with the
$pushoperator to append multiple values to an array<field>.The
$pushoperator can use$eachmodifier with other modifiers. For a list of modifiers available for$push, see Modifiers.
Examples¶
Use $each with $push Operator¶
The following example appends each element of [ 90, 92, 85 ] to
the scores array for the document where the name field
equals joe:
Use $each with $addToSet Operator¶
A collection inventory has the following document:
Then the following operation uses the $addToSet operator
with the $each modifier to add multiple elements to the
tags array:
The operation adds only "camera" and "accessories" to the
tags array since "electronics" already exists in the array: