MongoDB数据更新可以使用update()
函数。
db.collection.update({<where>},{<document>})
- criteria :
update
的查询条件,类似sql update
查询内where
后面的。 - objNew :
update
的对象和一些更新的操作符(如$,$inc...
)等,也可以理解为sql update
查询内set
后面的 - upsert : 这个参数的意思是,如果不存在
update
的记录,是否插入objNew
,true为插入,默认是false,不插入。 - multi : mongodb默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。
db.userdetails.update(
{
"user_id" : "QRSTBWN"
},
{
"user_id" : "QRSTBWN",
"password" :"NEWPASSWORD",
"date_of_join" : "17/10/2010",
"education" :"M.B.A.",
"profession" : "MARKETING",
"interest" :"MUSIC",
"community_name" :[
"MODERN MUSIC",
"CLASSICAL MUSIC",
"WESTERN MUSIC"
],
}
)
async update(user) {
const {ctx} = this;
user.password = ctx.helper.encrypt(user.password);
return await ctx.model.User.update({'_id':user._id},user);
}