【MongoDB】Update Operators

【MongoDB】修改操作符 Update Operators

文档格式如下

{
   <operator1>: { <field1>: <value1>, ... },
   <operator2>: { <field2>: <value2>, ... },
   ...
}

Update Operators

1. 字段操作

NameDescription
$currentDateSets the value of a field to current date, either as a Date or a Timestamp.
将字段的值设置为当前日期,可以是 Date 或 Timestamp。
$incIncrements the value of the field by the specified amount.
将字段的值按给定的amount增加
{¥inc{: , : …}}
$minOnly updates the field if the specified value is less than the existing field value.
仅当指定值小于现有字段值时才更新字段。
$maxOnly updates the field if the specified value is greater than the existing field value.
仅当指定值大于现有字段值时才更新字段。
$mulMultiplies the value of the field by the specified amount.
将字段的值按给定的amount相乘
$renameRenames a field.
给字段重命名
$setSets the value of a field in a document.
设置文档中某个字段的值
$setOnInsertSets the value of a field if an update results in an insert of a document. Has no effect on update operations that modify existing documents.
如果update导致文档的insert,则设置字段的值。 对修改现有文档的更新操作没有影响。
$unsetRemoves the specified field from a document.
从文档中删除指定指定的字段

2. 数组修改器

2.1 Operators
NameDescription
$Acts as a placeholder to update the first element that matches the query condition.
作为占位符更新第一个匹配查询条件的元素。
$[]Acts as a placeholder to update all elements in an array for the documents that match the query condition.
充当占位符,用于更新数组中与查询条件匹配的文档的所有元素。
$[<元素名>]Acts as a placeholder to update all elements that match the arrayFilters condition for the documents that match the query condition.
充当占位符,为与查询条件匹配的文档更新所有与 arrayFilters 条件匹配的元素。
$addToSetAdds elements to an array only if they do not already exist in the set.
如果数组中没有这些元素,那么则向数组中添加这些元素
$pop删除数组中的第一个或最后一个元素(传值-1:删第一个,传值1:删最后一个)
$pullRemoves all array elements that match a specified query
移除所有匹配指定查询的数组元素
$pushAdds an item to an array.
添加指定的值到数组中
$pushAllRemoves all matching values from an array.
在数组中删除所有匹配的值
2.2 Modifiers
NameDescription
$eachModifies the push and addToSet operators to append multiple items for array updates.
修改 push 和 addToSet 运算符以附加多个元素以进行数组更新。
$positionModifies the push operator to specify the position in the array to add elements.
修改 push 操作符以指定在数组中的位置添加元素。
$sliceModifies the push operator to limit the size of updated arrays.
修改 push 操作符以限制更新数组的大小。
$sortModifies the push operator to reorder documents stored in an array.
修改 push 操作符以对存储在数组中的文档进行重新排序。

3. Bitwise

NameDescription
$bitPerforms bitwise AND, OR, and XOR updates of integer values.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值