Created
December 16, 2017 12:23
-
-
Save asduser/2b78547a74986cc9a533f9d9adba6d9a to your computer and use it in GitHub Desktop.
MongoDb - update user in another database from the root.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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