$set
是默认行为,通常省略不写。
在众触,通常是更新x
里的详细信息,因此x.
也默认不写。
比如把{ $set: { "x.年龄": 18 } }
简写成{ 年龄: 18 }
更新y
里的内容时y.
是不能省略的。其它字段不能直接更新。
更新器updater
里未出现的字段是不会被更新的,即使用户在表单中删除了此字段。可以在更新器中用$unset
明确删除,通常更简单的做法是在modify()
函数中把参数option
设为true
或1
表示覆盖整个x
字段操作符
$set
| 设置文档中字段的值。 Sets the value of a field in a document. { $set: { "x.arr.0.total": 9, "tags.1": "rain gear" } } |
$unset
| 从文档中删除指定的字段。 Removes the specified field from a document. { $unset: { quantity: "", instock: "" } } |
$inc
| 将字段的值增加指定的数量。 Increments the value of the field by the specified amount. { $inc: { "x.arr.0.total": 1, quantity: -2 } } |
$mul
| 将字段的值乘以指定的数量。 Multiplies the value of the field by the specified amount. { $mul: { price: 1.25 } } |
$rename
| 重命名字段。 Renames a field. => $unset + $set { $rename: { "name.first": "name.fname", cell: "mobile" } } |
$currentDate
| 将字段的值设置为当前日期,即日期或时间戳。 Sets the value of a field to current date, either as a Date (default) or a Timestamp. { $currentDate: { lastModified: true, "cancellation.date": { $type: "timestamp" } } } |
$min
| 仅当指定值小于现有字段值时才更新该字段。 Only updates the field if the specified value is less than the existing field value. { $min: { lowScore: 150 } } |
$max
| 仅当指定值大于现有字段值时才更新该字段。 Only updates the field if the specified value is greater than the existing field value. { $max: { highScore: 870 } } |
数组操作符
$push
| 将项目添加到数组。 Adds an item to an array. { $push: { arr: $l.O } } |
$addToSet
| 仅当元素不存在于集合中时才将它们添加到数组中。Set是指不可重复的集合 Adds elements to an array only if they do not already exist in the set. |
$pop
| 删除数组的第一项或最后一项。 Removes the first or last item of an array. { $pop: { scores: -1, books: 1 } } 删除scores数组中的第一个元素,删除books数组中的最后一个元素 |
$pull
| 删除与指定查询匹配的所有数组元素。 Removes all array elements that match a specified query. { $pull: { votes: { $gt: 6 } } } 移除votes数组中大于6的数字 { $pull: { results: { item: "B", score: 8 } } } 移除results数组中item == "B" && score == 8的对象 |
$pullAll
| 从数组中删除所有匹配的值。 Removes all matching values from an array. { $pullAll: { scores: [ 0, 5 ] } } 移除scores数组中的所有0和5 |
$ *
| 更新与查询条件匹配的第一个元素。查询条件必须包含待更新数组字段名。 A placeholder to update the first match element of query. must include the array field in query. $xdb.modify(_id, { $set: { "x.arr.$": 1 } }, { arrayQueries: { "x.arr": 0 } }) $xdb.modify(_id, { $inc: { "x.arr.$.no": 1 } }, { arrayQueries: { "x.arr": { $elemMatch: { a: a, b: {$gt: 5} }}}}) 用$elemMatch指定数组元素的多个条件 |
$[]
| 更新数组中的所有元素。 A placeholder to update all elements in an array. $xdb.modify(_id, { $inc: { "x.arr.$[].price": 1 } }) |
$[filterName]
*
| 更新与过滤条件arrayFilters 匹配的所有元素。 use the filtered positional operator to match all embedded array elements that match the specified criteria 有文档:{ id: 1, items: [{ id: 2, blocks: [ { id: 3, txt: "hello" } ] }]} $xdb.modify(_id, { $set: { "items.$[item].blocks.$[block].txt": "hi" } }, { arrayFilters: [{ "item.id": 2 }, { "block.id": 3 }] }) |
数组修饰符
$each
| 修饰$push 和$addToSet 运算符以附加多个项以进行数组更新。 Modifies the $push and $addToSet operators to append multiple items for array updates. { $push: { scores: { $each: [ 90, 92, 85 ] } } } 给数组scores追加三项 |
$position
| 修饰$push 运算符以指定要添加元素的数组中的位置。 Modifies the $push operator to specify the position in the array to add elements. { $push: { scores: { $each: [ 90, 92, 85 ], $position: 0 } } } 插入到队首 |
$slice
| 修饰$push 运算符以限制更新数组的大小。 Modifies the $push operator to limit the size of updated arrays. { $push: { "y.arr": { $each: [], $slice: -10 } } } 截取数组的后10位 |
$sort
| 修饰$push 运算符以对存储在数组中的文档重新排序。 Modifies the $push operator to reorder documents stored in an array. { $push: { "y.arr": { $each: [{ name: “Ken“, score: 85 }], $sort: { score: -1 }, $slice: 10 } } } 插入新元素后按成绩降序排序并截取数组的前10位 |
option
可以是基本类型,如果为真时表示用updater
覆盖整个x
字段。
也可以是对象类型。当使用$
占位符来更新数组的第一个元素时用arrayQueries
来指定数组的查询条件;当使用$[filterName]
占位符来更新与过滤条件匹配的所有元素时用arrayFilters
来指定数组的过滤条件。