Skip to content

Instantly share code, notes, and snippets.

@asduser
Created December 16, 2017 12:23
Show Gist options
  • Save asduser/2b78547a74986cc9a533f9d9adba6d9a to your computer and use it in GitHub Desktop.
Save asduser/2b78547a74986cc9a533f9d9adba6d9a to your computer and use it in GitHub Desktop.
MongoDb - update user in another database from the root.
MongoClient.connect('mongodb://admin:pass@localhost:27017/mydb?authSource=admin').then((db) => {
return db.command({
grantRolesToUser: 'user',
roles: ['dbAdmin', 'readWrite']
})
.then((stat) => {
console.log(stat);
});
})
.catch(console.log);
MongoClient.connect('mongodb://user:pass@localhost:27017/mydb').then((db) => {
console.log('Connected!');
return db.command({
usersInfo: 'user',
})
.then(({users}) => {
const user = users[0];
user.roles.forEach((r) => {
console.log(r);
});
})
.then(() => {
return db.collection('people').insertOne({ name: 'bob', age: 20 });
})
.then(() => {
return db.collection('people').find().toArray()
.then((people) => {
console.log(people);
});
});
})
.catch(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment