简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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