简体   繁体   English

使用 mongodb 更新哈希密码

[英]Update hash password with mongodb

I'm trying to update my user document to change the password but mongodb ignore the field password when I pass a bcrypt hashed password as $2a$10$lnTLIgnOWuoolOkqBfzcd.0pNLFtstX20p8KJNQmKkL.6D.W7Zu0a我正在尝试更新我的用户文档以更改密码,但是当我将 bcrypt 哈希密码作为$2a$10$lnTLIgnOWuoolOkqBfzcd.0pNLFtstX20p8KJNQmKkL.6D.W7Zu0a传递时,mongodb 忽略字段密码

If I remove the first $ that work.如果我删除第一个$工作。

my update func我的更新功能

func (r Repo) Update(id string, updUsr interface{}) (User, error) {
    // uid work

    filter := bson.M{"_id": bson.M{"$eq": uid}, "deleted_at": nil}
    update := []bson.D{
        {primitive.E{
            Key:   "$set",
            Value: updUsr,
        }},
        {primitive.E{
            Key: "$addFields",
            Value: bson.D{primitive.E{
                Key:   "modified_at",
                Value: time.Now(),
            }},
        }},
    }

    res := r.col.FindOneAndUpdate(
        r.ctx,
        filter,
        update,
    )
    if res.Err() != nil {
        // err
    }

    // decode work

    return u, nil
}

my update func call我的更新函数调用

// doesn't work
updPwd := password{
    Password: "$2a$10$lnTLIgnOWuoolOkqBfzcd.0pNLFtstX20p8KJNQmKkL.6D.W7Zu0a",
}

// working version 
updPwd := password{
    Password: "2a$10$lnTLIgnOWuoolOkqBfzcd.0pNLFtstX20p8KJNQmKkL.6D.W7Zu0a",
}

_, err = us.Update(uid, updPwd)
if err != nil {
    // err
}

I don't know how to passe my hash variable to mongodb actually that just remove my password key because I proccess like password is an empty value and if I remove the first $ sign my update is successfully.我不知道如何将我的哈希变量传递给 mongodb 实际上只是删除我的密码密钥,因为我处理密码是一个空值,如果我删除第一个$符号我的更新成功。

Every review is appreciate !每条评论都值得赞赏!

Thank谢谢

In an update operation, a string value that begins with a dollar sign $ is treated as a variable reference and replaced with the corresponding value.在更新操作中,以美元符号$开头的字符串值被视为变量引用并替换为相应的值。

You will likely need to use the $literal operator for that string to be treated as a value instead of a variable.您可能需要使用$literal运算符将该字符串视为值而不是变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM