To add MD5 hash value, use hex_md5(). Let us first create a collection with documents −
>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") }
Following is the query to display all documents from a collection with the help of find() method −
> db.addMd5HashValueDemo.find();
This will produce the following output −
{ "_id" : ObjectId("5cd6a4c66d78f205348bc619"), "UserName" : "Adam", "UserPassword" : "Adam123456" } { "_id" : ObjectId("5cd6a4e46d78f205348bc61a"), "UserName" : "Chris", "UserPassword" : "Chris_121#" }
Following is the query to add md5 hash value to mongo collection −
> db.addMd5HashValueDemo.find().forEach( function(documentPass){ documentPass.Value = hex_md5(documentPass.UserPassword); db.addMd5HashValueDemo.save(documentPass); });
Let us check the document once again −
> db.addMd5HashValueDemo.find();
This will produce the following output −
{ "_id" : ObjectId("5cd6a4c66d78f205348bc619"), "UserName" : "Adam", "UserPassword" : "Adam123456", "Value" : "6523857c2bf79b63fd5fa0322575f7be" } { "_id" : ObjectId("5cd6a4e46d78f205348bc61a"), "UserName" : "Chris", "UserPassword" : "Chris_121#", "Value" : "3391ccbe33624258cafa23aa50301615" }