mongodb addUser code example

Example 1: mongodb add user

use 
db.createUser({
  user: '',
  pwd: passwordPrompt(),
  roles: [{
    role: 'readWrite',
    db: ''
  }]
})

Example 2: mongodb create user

use admin
db.createUser(
   {
     user: "restricted",
     pwd: passwordPrompt(),      // Or  ""
     roles: [ { role: "readWrite", db: "reporting" } ],
     authenticationRestrictions: [ {
        clientSource: ["192.0.2.0"],
        serverAddress: ["198.51.100.0"]
     } ]
   }
)

Example 3: mongodb add admin user

use admin
db.createUser({
  user: "username",
  pwd: "password",
  roles: [ "root" ]
})

Tags:

Misc Example