Skip to content

Latest commit

 

History

History
50 lines (45 loc) · 1.47 KB

update.md

File metadata and controls

50 lines (45 loc) · 1.47 KB

eggjs mongodb Update

MongoDB 更新文档

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"
            ],
    }
)

egg操作mongodb

async update(user) {
    const {ctx} = this;
    user.password = ctx.helper.encrypt(user.password);
    return await ctx.model.User.update({'_id':user._id},user);
}

参考资料

update