MongoDB 更新运算符
以下修饰符可用于更新操作。例如-在db.collection.update()和db.collection.findAndModify()中。
在以下格式的文档中定义运算符表达式:
{
<operator1>: { <field1>: <value1>, ... },
<operator2>: { <field2>: <value2>, ... },
}
字段运算符
$currentDate
它将字段的元素更新为当前日期,即日期或日期。时间戳记。此运算符的默认数据类型是日期。
语法:
{ $currentDate: { <field1>: <typeSpecification1>, ... } }
示例:
db.books.insertOne(
{ _id: 1, status: "a", lastModified: purchaseDate("2013-10-02T01:11:18.965Z") }
)
$inc
它将文件增加指定的值。
语法:
{ $inc: { <field1>: <amount1>, <field2>: <amount2>, ... } }
示例:
{
_id: 000438,
sku: "MongoDB",
quantity: 1,
metrics: {
orders: 2,
ratings: 3.5
}
}
$min
如果指定值小于字段的当前值,它将字段的值更改为指定值。
语法:
{ $min: { <field1>: <value1>, ... } }
示例:
{ _id: 0021, highprice: 800, lowprice: 200 }
db.books.update( { _id: 0021 }, { $min: { highprice: 500 } } )
$max
如果指定值大于字段的当前值,它将字段的值更改为指定值。
语法:
{ $max: { <field1>: <value1>, ... } }
示例:
{ _id: 0021, highprice: 800, lowprice: 200 }
db.books.update( { _id: 0021 }, { $max: { highprice: 950 } } )
$mul
它将字段的值乘以数字。
语法:
{ $mul: { <field1>: <number1>, ... } }
示例:
db.books.update(
{ _id: 1 },
{ $mul: { price: NumberDecimal("180.25"), qty: 2 } }
)
$rename
重命名运算符更改字段的名称。
语法:
{$rename: { <field1>: <newName1>, <field2>: <newName2>, ... } }
示例:
db.books.updateMany( {}, { $rename: { "nmae": "name" } } )
$set
集合运算符使用指定的值更改字段的值。
语法:
{ $set: { <field1>: <value1>, ... } }
示例:
{
_id: 100,
sku: "abc123",
quantity: 50,
instock: true,
reorder: false,
details: { model: "14Q2", make: "xyz" },
tags: [ "technical", "non technical" ],
ratings: [ { by: "ijk", rating: 4 } ]
}
$setOnInsert
如果upsert设置为true,则会导致插入文档,然后setOnInsert运算符将指定的值分配给文档中的字段。
语法:
db.collection.update(
<query>,
{ $setOnInsert: { <field1>: <value1>, ... } },
{ upsert: true }
)
$unset
它将删除指定的字段。
语法:
{ $unset: { <field1>: "", ... } }
示例:
db.products.update(
{ sku: "unknown" },
{ $unset: { quantity: "", instock: "" } }
)
数组运算符
$
我们可以在不显式指定元素位置的情况下更新数组中的元素。
语法:
示例:
db.collection.update(
{ <array>: value ... },
{ <update operator>: { "<array>.$" : value } }
)
$[]
位置运算符指示更新运算符应更改给定数组字段中的所有元素。
语法:
{ <update operator>: { "<array>.$[]" : value } }
示例:
db.collection.updateMany(
{ <query conditions> },
{ <update operator>: { "<array>.$[]" : value } }
)
$[
]
它被称为过滤位置运算符,用于标识数组元素。
语法:
{ <update operator>: { "<array>.$[<identifier>]" : value } },
{ arrayFilters: [ { <identifier>: <condition> } ] }
示例:
db.collection.updateMany( { <query conditions> },
{ <update operator>: { "<array>.$[<identifier>]" : value } },
{ arrayFilters: [ { <identifier>: <condition> } ] } )
$addToSet
它将元素添加到数组中,除非该元素已经存在,在这种情况下,此运算符对该数组不执行任何操作。
语法:
{ $addToSet: { <field1>: <value1>, ... } }
示例:
db.books.update(
{ _id: 1 },
{ $addToSet: { tags: "MongoDB" } }
)
$pop
我们可以使用pop运算符删除数组的第一个或最后一个元素。我们需要将pop的值传递为-1,以删除数组的第一个元素,并传递1以删除数组的最后一个元素。
语法:
{ $pop: { <field>: <-1 | 1>, ... } }
示例:
db.books.update( { _id: 1 }, { $pop: { mongoDB: -1 } } )
$pull
使用pull运算符,我们可以删除数组中符合指定条件的值的所有实例。
语法:
{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }
示例:
db.books.update( { }, { $pull: { Development: { $in:["Java", "RDBMS" ] }, Tech: "Cybersecurity" } },
{ multi: true }
)
$push
它将指定值附加到数组。
语法:
{ $push: { <field1>: <value1>, ... } }
示例:
db.students.update( { _id: 9 }, { $push: { scores: 91 } } )
$pullAll
我们可以使用pullAll运算符从现有数组中删除指定值的所有实例。它将删除与列出的值匹配的元素。
语法:
{ $pullAll: { <field1>: [ <value1>, <value2> ... ], ... } }
示例:
db.survey.update( { _id: 1 }, { $pullAll: { scores: [ 0, 5 ] } } )
修饰符
$each
它与$addToSet运算符和$push运算符一起使用。如果字段中不存在该值,则可与addToSet运算符一起使用,以将多个值添加到数组中。
语法:
{ $addToSet: { <field>: { $each: [ <value1>, <value2> ... ] } } }
它与push运算符一起使用,可以将多个值附加到数组。
语法:
{ $push: { <field>: { $each: [ <value1>, <value2> ... ] } } }
示例:
db.students.update( { name: "Akki" }, { $push: { scores: { $each: [ 90, 92, 85 ] } } } )
$position
它指定push运算符在数组内插入元素的位置。
语法:
{
$push: {
<field>: {
$each: [ <value1>, <value2>, ... ],
$position: <num>
}
}
}
示例:
db.students.update(
{ _id: 1 },
{
$push: {
scores: {
$each: [ 50, 60, 70 ],
$position: 0
}
}
}
)
$slice
此修饰符用于在推入操作期间限制数组元素的数量。
语法:
{
$push: {
<field>: {
$each: [ <value1>, <value2>, ... ],
$slice: <num>
}
}
}
示例:
db.students.update(
{ _id: 1 },
{
$push: {
scores: {
$each: [ 80, 78, 86 ],
$slice: -5
}
}
}
)
$sort
sort修饰符在推入操作期间排列数组的值。
语法:
{
$push: {
<field>: {
$each: [ <value1>, <value2>, ... ],
$sort: <sort specification>
}
}
}
示例:
db.students.update(
{ _id: 1 },
{
$push: {
quizzes: {
$each: [ { id: 3, score: 8 }, { id: 4, score: 7 }, { id: 5, score: 6 } ],
$sort: { score: 1 }
}
}
}
)
按位运算符
$bit
位运算符使用按位运算来更新字段。它支持按位与,按位OR和按位XOR运算。
语法:
{ $bit: { <field>: { <and|or|xor>: <int> } } }
示例:
db.books.update( { _id: 1 }, { $bit: { expdata: { and: price(100) } } }
)