On this page本页内容
$each¶The $each modifier is available for use with the $addToSet operator and the $push operator.$each修饰符可与$addToSet运算符和$push运算符一起使用。
Use with the 如果数组$addToSet operator to add multiple values to an array <field> if the values do not exist in the <field>.<field>中不存在多个值,则与$addToSet运算符一起使用可将这些值添加到数组<field>。
Use with the 与$push operator to append multiple values to an array <field>.$push运算符一起使用可将多个值附加到数组<field>。
The $push operator can use $each modifier with other modifiers. $push运算符可以将$each修饰符与其他修饰符一起使用。For a list of modifiers available for 有关$push, see Modifiers.$push可用的修饰符列表,请参阅修饰符。
$each with $push Operator$push运算符一起使用$each¶The following example appends each element of 下面的示例将[ 90, 92, 85 ] to the scores array for the document where the name field equals joe:[90,92,85]中的每个元素添加到文档的scores数组中,其中name字段等于joe:
$each with $addToSet Operator$each与$addToSet运算符一起使用¶A collection 集合inventory has the following document:inventory包含以下文档:
Then the following operation uses the 然后,以下操作使用$addToSet operator with the $each modifier to add multiple elements to the tags array:$addToSet运算符和$each修饰符将多个元素添加到标记数组中:
The operation adds only 该操作仅向"camera" and "accessories" to the tags array since "electronics" already exists in the array:tags数组添加"camera"和"accessories",因为该数组中已经存在"electronics":