5、db.update(query, update, options, callback)
作用:
根据 update 参数的规则,更新匹配到 query 的结果集。
参数:
query: 与 find 和 findOne 中 query 参数的用法一致
update: 指定文档更改规则。该参数可以是一个新的文档,也可以是一套修饰符,两者不能同时使用。使用修饰符时,如果需要更改的字段不存在,将会自动创建。可用的修饰符有 $set(改变字段值), $unset(删除某一字段), $inc(增加某一字段), $min/$max(改变字段值,传入值需要小于/大于当前值), 还有一些用在数组上的修饰符,$push, $pop, $addTopSet, $pull, $each, $slice,具体用法如下示例。
options: object 类型。muti(默认 false),是否允许修改多条文档;upsert(默认为 false),如果 query 没有匹配到结果集,有两种情况需要考虑,一个是 update 是一个简单的对象 (不包含任何修饰符),另一种情况是带有修饰符,对第一种情况会直接将该文档插入,对第二种情况会将通过修饰符更改后的文档插入;
callback(可选): 参数 (err, numAffected, affectedDocuments, upsert)。numAffected:被影响的文档个数;affectedDocuments:更新后的文档。
注意:_id 不能被修改
示例:
// 文档集
// { _id: 'id1', planet: 'Mars', system: 'solar', inhabited: false }
// { _id: 'id2', planet: 'Earth', system: 'solar', inhabited: true }
// { _id: 'id3', planet: 'Jupiter', system: 'solar', inhabited: false }
// { _id: 'id4', planet: 'Omicron Persia 8', system: 'futurama', inhabited: true }
// 用一个文档替换另一个文档
db.update({planet:'Jupiter'},{planet:'Pluton'},{},function(err,numReplaced){
// numReplaced = 1
// The doc #3 has been replaced by { _id: 'id3', planet: 'Pluton' }
// Note that the _id is kept unchanged, and the document has been replaced
// (the 'system' and inhabited fields are not here anymore)
});
// 设定一个已存字段的值
db.update({system:'solar'},{$set:{system:'solar system'}},{multi:true},function(err,numReplaced){
// numReplaced = 3
// Field 'system' on Mars, Earth, Jupiter now has value 'solar system'
});
// 设定一个不存在字段的值
db.update({planet:'Mars'},{$set:{"data.satellites":2,"data.red":true}},{},function(){
// Mars document now is { _id: 'id1', system: 'solar', inhabited: false
// , data: { satellites: 2, red: true }
// }
// Not that to set fields in subdocuments, you HAVE to use dot-notation
// Using object-notation will just replace the top-level field
db.update({planet:'Mars'},{$set:{data:{satellites:3}}},{},function(){
// Mars document now is { _id: 'id1', system: 'solar', inhabited: false
// , data: { satellites: 3 }
// }
// You lost the "data.red" field which is probably not the intended behavior
});
});
// 删除一个字段
db.update({planet:'Mars'},{$unset:{planet:true}},{},function(){
// Now the document for Mars doesn't contain the planet field
// You can unset nested fields with the dot notation of course
});
// 设置upsert
db.update({planet:'Pluton'},{planet:'Pluton',inhabited:false},{upsert:true},function(err,numReplaced,upsert){
// numReplaced = 1, upsert = { _id: 'id5', planet: 'Pluton', inhabited: false }
// A new document { _id: 'id5', planet: 'Pluton', inhabited: false } has been added to the collection
});
// If you upsert with a modifier, the upserted doc is the query modified by the modifier
// This is simpler than it sounds :)
db.update({planet:'Pluton'},{$inc:{distance:38}},{upsert:true},function(){
// A new document { _id: 'id5', planet: 'Pluton', distance: 38 } has been added to the collection
});
// If we insert a new document { _id: 'id6', fruits: ['apple', 'orange', 'pear'] } in the collection,
// let's see how we can modify the array field atomically
// $push inserts new elements at the end of the array
db.update({_id:'id6'},{$push:{fruits:'banana'}},{},function(){
// Now the fruits array is ['apple', 'orange', 'pear', 'banana']
});
// $pop removes an element from the end (if used with 1) or the front (if used with -1) of the array
db.update({_id:'id6'},{$pop:{fruits:1}},{},function(){
// Now the fruits array is ['apple', 'orange']
// With { $pop: { fruits: -1 } }, it would have been ['orange', 'pear']
});
// $addToSet adds an element to an array only if it isn't already in it
// Equality is deep-checked (i.e. $addToSet will not insert an object in an array already containing the same object)
// Note that it doesn't check whether the array contained duplicates before or not
db.update({_id:'id6'},{$addToSet:{fruits:'apple'}},{},function(){
// The fruits array didn't change
// If we had used a fruit not in the array, e.g. 'banana', it would have been added to the array
});
// $pull removes all values matching a value or even any NeDB query from the array
db.update({_id:'id6'},{$pull:{fruits:'apple'}},{},function(){
// Now the fruits array is ['orange', 'pear']
});
db.update({_id:'id6'},{$pull:{fruits:$in:['apple','pear']}},{},function(){
// Now the fruits array is ['orange']
});
// $each can be used to $push or $addToSet multiple values at once
// This example works the same way with $addToSet
db.update({_id:'id6'},{$push:{fruits:{$each:['banana','orange']}}},{},function(){
// Now the fruits array is ['apple', 'orange', 'pear', 'banana', 'orange']
});
// $slice can be used in cunjunction with $push and $each to limit the size of the resulting array.
// A value of 0 will update the array to an empty array. A positive value n will keep only the n first elements
// A negative value -n will keep only the last n elements.
// If $slice is specified but not $each, $each is set to []
db.update({_id:'id6'},{$push:{fruits:{$each:['banana'],$slice:2}}},{},function(){
// Now the fruits array is ['apple', 'orange']
});
// $min/$max to update only if provided value is less/greater than current value
// Let's say the database contains this document
// doc = { _id: 'id', name: 'Name', value: 5 }
db.update({_id:'id1'},{$min:{value:2}},{},function(){
// The document will be updated to { _id: 'id', name: 'Name', value: 2 }
});
db.update({_id:'id1'},{$min:{value:8}},{},function(){
// The document will not be modified
});
当前内容版权归 腾讯AlloyTeam 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 腾讯AlloyTeam .