MongoDB更新文档
MongoDB 使用 update() 和 save() 方法来更新集合中的文档。接下来让我们详细来看下两个函数的应用及其区别。
update() 方法
update() 方法用于更新已存在的文档。语法格式如下:
db.collection.update(
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>
}
)
参数说明:
query : update的查询条件,类似sql update查询内where后面的。
update : update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的
upsert : 可选,这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
multi : 可选,mongodb 默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。
writeConcern :可选,抛出异常的级别。
使用update方法更新age
> db.user_info.find() { "_id" : ObjectId("5de4835bfc0c5f2c5c98a97a"), "name" : "zhangsan", "age" : "30", "adress" : "zhenzhou" } { "_id" : ObjectId("5de48386fc0c5f2c5c98a97b"), "name" : "wangwu", "age" : "25", "adress" : "beijing" } { "_id" : ObjectId("5de483abfc0c5f2c5c98a97c"), "name" : "zhangsan", "age" : "18", "adress" : "shanghai" } > db.user_info.update({'name':'wangwu'},{$set:{'name':'lisi'}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.user_info.find() { "_id" : ObjectId("5de4835bfc0c5f2c5c98a97a"), "name" : "zhangsan", "age" : "30", "adress" : "zhenzhou" } { "_id" : ObjectId("5de48386fc0c5f2c5c98a97b"), "name" : "lisi", "age" : "25", "adress" : "beijing" } { "_id" : ObjectId("5de483abfc0c5f2c5c98a97c"), "name" : "zhangsan", "age" : "18", "adress" : "shanghai" }
如果需要更改多条数据,需要加上multi 参数为 true
> db.user_info.find() { "_id" : ObjectId("5de4835bfc0c5f2c5c98a97a"), "name" : "zhangsan", "age" : "30", "adress" : "zhenzhou" } { "_id" : ObjectId("5de48386fc0c5f2c5c98a97b"), "name" : "lisi", "age" : "25", "adress" : "beijing" } { "_id" : ObjectId("5de483abfc0c5f2c5c98a97c"), "name" : "zhangsan", "age" : "18", "adress" : "shanghai" } > db.user_info.update({'name':'zhangsan'},{$set:{'name':'zhangwei'}},{multi:true}) WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 }) > db.user_info.find() { "_id" : ObjectId("5de4835bfc0c5f2c5c98a97a"), "name" : "zhangwei", "age" : "30", "adress" : "zhenzhou" } { "_id" : ObjectId("5de48386fc0c5f2c5c98a97b"), "name" : "lisi", "age" : "25", "adress" : "beijing" } { "_id" : ObjectId("5de483abfc0c5f2c5c98a97c"), "name" : "zhangwei", "age" : "18", "adress" : "shanghai" }
save() 方法
save() 方法通过传入的文档来替换已有文档。语法格式如下:
db.collection.save(
<document>,
{
writeConcern: <document>
}
)
参数说明:
document : 文档数据。
writeConcern :可选,抛出异常的级别。
> db.user_info.find() { "_id" : ObjectId("5de4835bfc0c5f2c5c98a97a"), "name" : "zhangwei", "age" : "30", "adress" : "zhenzhou" } { "_id" : ObjectId("5de48386fc0c5f2c5c98a97b"), "name" : "lisi", "age" : "25", "adress" : "beijing" } { "_id" : ObjectId("5de483abfc0c5f2c5c98a97c"), "name" : "zhangwei", "age" : "18", "adress" : "shanghai" } > > db.user_info.save({ "_id" : ObjectId("5de483abfc0c5f2c5c98a97c"), "name" : "wangwu", "age" : "20", "adress" : "zhumadian" }) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.user_info.find() { "_id" : ObjectId("5de4835bfc0c5f2c5c98a97a"), "name" : "zhangwei", "age" : "30", "adress" : "zhenzhou" } { "_id" : ObjectId("5de48386fc0c5f2c5c98a97b"), "name" : "lisi", "age" : "25", "adress" : "beijing" } { "_id" : ObjectId("5de483abfc0c5f2c5c98a97c"), "name" : "wangwu", "age" : "20", "adress" : "zhumadian" }
天下难事,必作于易;天下大事,必作于细