Navigation
This version of the documentation is archived and no longer supported.

$each

Note

The $each modifier is only used with the $addToSet and $push operators. See the documentation of $addToSet and $push for more information.

$each

The $each modifier is available for use with the $addToSet operator and the $push operator.

Use the $each modifier with the $addToSet operator to add multiple values to an array <field> if the values do not exist in the <field>.

db.collection.update( <query>,
                      {
                        $addToSet: { <field>: { $each: [ <value1>, <value2> ... ] } }
                      }
                    )

Use the $each modifier with the $push operator to append multiple values to an array <field>.

db.collection.update( <query>,
                      {
                        $push: { <field>: { $each: [ <value1>, <value2> ... ] } }
                      }
                    )

Changed in version 2.4: MongoDB adds support for the $each modifier to the $push operator. The $push operator can use $each in conjunction with $slice and $sort modifiers. When used in conjunction with the other modifiers, $each must be the first modifier.

←   $push $slice  →