I want to make a Mongo FindAndModify request which finds a user by email and password and then updates it in one request, to be atomic. With basic MD5 hashed passwords and predefined salt I'd calculate hash before a request and make a following request:
db.people.findAndModify( {
query: { email: "a@a.com", password: "<hashed value>"},
update: { $set: { myField: "myNewValue" } }
} );
But bcrypt passwords are not the same every time because different salts are used on each hash generation so this approach doesn't work. Currently I have to find a user just by email and check his password in code with BCrypt.checkPassword function and, if it returns true, update my value in database. So I end up doing 2 requests and it's not atomic in Mongo.
So is there a way to do that in one MongoDB request (compare with bcrypt hash and update)? (I've seen an article on Mongo website about how to manually implemente 2-phase commit but wondering if there is a clean and nice solution).