Model.update() 更新数据库中的一个文档而不返回它。(已被弃用)
Model.updateOne() 更新匹配的第一个文档
所有
匹配的文档
Model.replaceOne() 用给定的文档替换现有文档
var query = { name: 'borne' };
Model.update(query, { name: 'jason bourne' }, options, callback)
// is sent as
Model.update(query, { $set: { name: 'jason bourne' }}, options, callback)
// if overwrite option is false. If overwrite is true, sent without the $set wrapper.
MongoDB数组更新操作each修饰符
// 如下操作添加数组["c", "d"]到字段letters
// 原数据 { _id: 1, letters: ["a", "b"] }
test.update(
{ _id: 1 },
{ $addToSet: {letters: [ "c", "d" ] } }
)
{ _id: 1, letters: ["a", "b",[ "c", "d" ]] }
//如果想将数组中的每个元素分开添加到letters字段中,可以使用$each修饰符。
// 原数据 { _id: 1, letters: ["a", "b"] }
test.update(
{ _id: 1 },
{ $each: {letters: [ "c", "d","a" ] } }
)
//元素已经存在于数组中了的元素不会重复出现
{ _id: 1, letters: ["a", "b", "c", "d" ] }
slice子操作符的理解
// $push的$slice子操作符的理解
// person.update(
// {"name":"123"},
// {"$push":{"text":{ "$each":["1","2","3","4"], "$slice":-4}}}
// )