将MD5哈希值添加到MongoDB集合?
要添加MD5哈希值,请使用hex_md5()。首先让我们创建一个包含文档的集合-
>db.addMd5HashValueDemo.insertOne({"UserName":"Adam","UserPassword":"Adam123456"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6a4c66d78f205348bc619") } >db.addMd5HashValueDemo.insertOne({"UserName":"Chris","UserPassword":"Chris_121#"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6a4e46d78f205348bc61a") }
以下是在find()
方法的帮助下显示集合中所有文档的查询-
> db.addMd5HashValueDemo.find();
这将产生以下输出-
{ "_id" : ObjectId("5cd6a4c66d78f205348bc619"), "UserName" : "Adam", "UserPassword" : "Adam123456" } { "_id" : ObjectId("5cd6a4e46d78f205348bc61a"), "UserName" : "Chris", "UserPassword" : "Chris_121#" }
以下是将md5哈希值添加到mongo集合的查询-
> db.addMd5HashValueDemo.find().forEach( function(documentPass){ documentPass.Value = hex_md5(documentPass.UserPassword); db.addMd5HashValueDemo.save(documentPass); });
让我们再次检查文档-
> db.addMd5HashValueDemo.find();
这将产生以下输出-
{ "_id" : ObjectId("5cd6a4c66d78f205348bc619"), "UserName" : "Adam", "UserPassword" : "Adam123456", "Value" : "6523857c2bf79b63fd5fa0322575f7be" } { "_id" : ObjectId("5cd6a4e46d78f205348bc61a"), "UserName" : "Chris", "UserPassword" : "Chris_121#", "Value" : "3391ccbe33624258cafa23aa50301615" }