Use mongo shell to change user password

Hi,

I have a database called “mydb.” In it there is a collection called “users,” which is where my users are stored.
I don’t have the mail server setup on the VPS I am using so I cannot use the reset password feature in my meteor app.

How do a change a user email using command line? I tried

mongo
use mydb
db.users.changeUserPassword(“user”, “newPwd”)

but that did not work and I couldn’t find much info online. Any help is much appreciated. I am using Mongo shell v2.6.7

Kamal

1 Like

Can’t you use meteor shell?

$ meteor shell
> Accounts.setPassword("userById", "new password", { logout: true });
4 Likes

The VPS does not have meteor installed on it. I did a meteor build on a test machine and am running the app on a production machine using node

it looks like meteor uses bcrypt from npm so I think you might be able to create a bcrypt with that module then use that to update it, no?

db.users.update({ _id: "someuserid" }, {
  services: {
    password: {
      bcrypt: "your generated bcrypt"
    }
  }
});

I seem to be able to do it on my system, not sure if it’s a good idea though.

3 Likes

Thanks for your tips, Corvid. I was able able to login successfully.

This needs to have the addition that what is hashed using bcrypt is the sha256 of the password SHA256(password)
I know this too old but I think this is important and worth the bump …

So you can install this bcrypt-cli globally like this :

$ npm install -g @carsondarling/bcrypt-cli

then use this to generate your bcrypted password

$ bcrypt $(echo -n "yourPasswordHere" | sha256sum | cut -d " " -f 1) && echo

and finally use mongo for the final blow …

$ mongo admin

and In mongo shell

> db.users.update({username: "yourUserName"}, {$set: { "services.password": { bcrypt: 'bcryptedPassword' } }})
7 Likes

Hi folks

I want to insert a new user from mongo using the following instruction

db.users.insert({
	createdAt: new Date(), 
	emails: { address: 'enzo@example.com'}, 
	services: {
	  password: { 
	  	bcrypt: "bcryptedPassword"}
	  	}
	}
);

I am using another user encrypted password, the authentication fail.

Any suggestion to improve my insert.

Thanks in advance

I’ve copied encrypted passwords from one account to another before, and the authentication works. I’m not sure why yours failed.

Why not use the API?

Hi @maxhodges

Thanks for your response.

I am brand new in meteor, I resolve my problem with Meteor shell, I just discover it

In Meteor shell I just run this command

Accounts.createUser({email: 'info@example.com', password: 'MyPassword'})
1 Like