0
0
MongoDBquery~20 mins

Creating users and roles in MongoDB - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB User and Role Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this user creation command?
Consider the following MongoDB command to create a user with readWrite role on the 'sales' database:

db.createUser({
  user: "salesUser",
  pwd: "pass123",
  roles: [{ role: "readWrite", db: "sales" }]
})

What will be the result of running this command if the user does not exist yet?
MongoDB
db.createUser({ user: "salesUser", pwd: "pass123", roles: [{ role: "readWrite", db: "sales" }] })
AUser 'salesUser' is created with readWrite access on 'sales' database and a success message is returned.
BSyntaxError: Missing parentheses in function call.
CAuthenticationFailed: Cannot create user without admin privileges.
DUser 'salesUser' is created but has no roles assigned.
Attempts:
2 left
💡 Hint
Creating a user with roles requires specifying the role and database correctly.
📝 Syntax
intermediate
2:00remaining
Which command correctly creates a role with read-only access on the 'inventory' database?
You want to create a custom role named 'inventoryRead' that allows reading any collection in the 'inventory' database. Which of the following commands is syntactically correct?
Adb.createRole({ role: "inventoryRead", privileges: [{ resource: { db: "inventory", collection: "all" }, actions: ["find"] }], roles: [] })
Bdb.createRole({ role: "inventoryRead", privileges: [{ resource: { db: "inventory" }, actions: ["find"] }], roles: [] })
Cdb.createRole({ role: "inventoryRead", privileges: [{ resource: { db: "inventory", collection: "" }, actions: ["find"] }], roles: [] })
Ddb.createRole({ name: "inventoryRead", privileges: [{ resource: { db: "inventory", collection: "*" }, actions: ["read"] }], roles: [] })
Attempts:
2 left
💡 Hint
The 'collection' field should be an empty string to specify all collections.
optimization
advanced
2:00remaining
How to optimize role assignment for multiple databases?
You want to create a user who has readWrite access on both 'sales' and 'marketing' databases. Which command is the most efficient and correct way to assign these roles during user creation?
Adb.createUser({ user: "multiDBUser", pwd: "pass456", roles: [{ role: "readWrite", db: "sales" }, { role: "readWrite", db: "marketing" }] })
Bdb.createUser({ user: "multiDBUser", pwd: "pass456", roles: [{ role: "readWrite", db: "sales,marketing" }] })
Cdb.createUser({ user: "multiDBUser", pwd: "pass456", roles: [{ role: "readWrite" }] })
Ddb.createUser({ user: "multiDBUser", pwd: "pass456", roles: ["readWrite"] })
Attempts:
2 left
💡 Hint
Each role must specify a single database.
🔧 Debug
advanced
2:00remaining
Why does this role creation command fail?
You run this command to create a role:

db.createRole({
  role: "dataAnalyst",
  privileges: [{ resource: { db: "analytics", collection: "reports" }, actions: ["find", "update"] }],
  roles: [{ role: "read", db: "analytics" }]
})

But it fails with an error. What is the most likely cause?
AThe 'actions' array contains 'update' which is not a valid action.
BThe 'roles' array references a role 'read' which is invalid; it should be 'readAnyDatabase' or 'readWrite'.
CThe role 'read' does not exist; it should be 'readAnyDatabase'.
DThe 'roles' array should be empty when privileges are specified.
Attempts:
2 left
💡 Hint
Check the validity of roles referenced inside the roles array.
🧠 Conceptual
expert
2:00remaining
What is the effect of assigning the 'root' role to a user?
In MongoDB, if you create a user and assign the built-in 'root' role, what does this role allow the user to do?
AThe user has read-only access to all databases.
BThe user can only read and write data on the admin database.
CThe user can only create and delete collections but cannot manage users.
DThe user can perform any action on any database, including user and role management.
Attempts:
2 left
💡 Hint
Think about the highest level of access in MongoDB.