Bird
Raised Fist0
MongoDBquery~10 mins

Creating users and roles in MongoDB - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a new user with read-only access to the 'sales' database.

MongoDB
db.createUser({ user: "readonlyUser", pwd: "password123", roles: [ { role: [1], db: "sales" } ] })
Drag options to blanks, or click blank then click option'
AreadWrite
BdbAdmin
Cread
DuserAdmin
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing 'readWrite' which allows writing, not just reading.
Using 'dbAdmin' which is for database administration, not just reading.
2fill in blank
medium

Complete the code to create a role that allows users to read and write on the 'inventory' database.

MongoDB
db.createRole({ role: "inventoryManager", privileges: [], roles: [ { role: [1], db: "inventory" } ] })
Drag options to blanks, or click blank then click option'
AreadWrite
BclusterAdmin
CdbAdmin
Dread
Attempts:
3 left
💡 Hint
Common Mistakes
Selecting 'read' which does not allow writing.
Choosing 'clusterAdmin' which is for cluster-wide administration.
3fill in blank
hard

Fix the error in the code to create a user with the 'userAdmin' role on the 'admin' database.

MongoDB
db.createUser({ user: "adminUser", pwd: "securePass", roles: [ { role: [1], db: "admin" } ] })
Drag options to blanks, or click blank then click option'
AreadWrite
Bread
CdbOwner
DuserAdmin
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'readWrite' which does not grant user management permissions.
Choosing 'read' which only allows reading data.
4fill in blank
hard

Fill both blanks to create a role 'reportViewer' that can read data and run aggregation on the 'reports' database.

MongoDB
db.createRole({ role: "reportViewer", privileges: [ { resource: { db: "reports", collection: "" }, actions: [ [1], [2] ] } ], roles: [] })
Drag options to blanks, or click blank then click option'
Afind
Baggregate
Cinsert
Dremove
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing 'insert' or 'remove' which allow modifying data.
Missing the 'aggregate' action needed for aggregation.
5fill in blank
hard

Fill all three blanks to create a user 'dataAnalyst' with a custom role that allows reading and running aggregation on the 'analytics' database.

MongoDB
db.createRole({ role: [1], privileges: [ { resource: { db: [2], collection: "" }, actions: [ "find", "aggregate" ] } ], roles: [] }); db.createUser({ user: "dataAnalyst", pwd: "pass456", roles: [ { role: [1], db: [2] } ] })
Drag options to blanks, or click blank then click option'
AanalyticsReader
Banalytics
Cread
DreadWrite
Attempts:
3 left
💡 Hint
Common Mistakes
Using built-in roles instead of the custom role name.
Mismatching the database name between user and role creation.

Practice

(1/5)
1. What is the primary purpose of creating users in MongoDB?
easy
A. To control who can access the database
B. To store data in collections
C. To optimize query performance
D. To backup the database automatically

Solution

  1. Step 1: Understand user creation in MongoDB

    Users in MongoDB are created to manage access to the database system.
  2. Step 2: Identify the purpose of users

    Users control who can connect and perform actions on the database.
  3. Final Answer:

    To control who can access the database -> Option A
  4. Quick Check:

    Users = Access control [OK]
Hint: Users manage access, not data or backups [OK]
Common Mistakes:
  • Confusing users with data storage
  • Thinking users optimize queries
  • Assuming users handle backups
2. Which of the following is the correct syntax to create a user with read-only access on the sales database in MongoDB?
easy
A. db.createUser({user: 'reader', pwd: 'pass', roles: [{role: 'read', db: 'sales'}]})
B. db.createUser({user: 'reader', pwd: 'pass', roles: ['read']})
C. db.addUser('reader', 'pass', 'read')
D. db.users.insert({user: 'reader', pwd: 'pass', role: 'read'})

Solution

  1. Step 1: Recall MongoDB user creation syntax

    The correct syntax uses db.createUser() with roles as an array of objects specifying role and database.
  2. Step 2: Check each option

    db.createUser({user: 'reader', pwd: 'pass', roles: [{role: 'read', db: 'sales'}]}) correctly specifies the role 'read' on the 'sales' database. db.createUser({user: 'reader', pwd: 'pass', roles: ['read']}) misses the database name, C uses deprecated syntax, D tries to insert directly into users collection which is invalid.
  3. Final Answer:

    db.createUser({user: 'reader', pwd: 'pass', roles: [{role: 'read', db: 'sales'}]}) -> Option A
  4. Quick Check:

    Roles need role and db fields [OK]
Hint: Roles need role and db fields in array [OK]
Common Mistakes:
  • Omitting the database in roles
  • Using deprecated addUser method
  • Trying to insert user directly into collection
3. Given the following command in MongoDB shell:
db.createUser({user: 'adminUser', pwd: 'secure123', roles: [{role: 'userAdminAnyDatabase', db: 'admin'}]})

What permission does the user adminUser have?
medium
A. Can read data from any database
B. Can manage users on any database
C. Can only read the admin database
D. Can write data to any database

Solution

  1. Step 1: Understand the role userAdminAnyDatabase

    This role allows managing users and roles across all databases.
  2. Step 2: Analyze the permissions given

    The user can create, modify, and delete users on any database but does not have read or write data permissions by default.
  3. Final Answer:

    Can manage users on any database -> Option B
  4. Quick Check:

    userAdminAnyDatabase = user management [OK]
Hint: userAdminAnyDatabase manages users, not data [OK]
Common Mistakes:
  • Assuming it grants read or write data access
  • Confusing admin database with all databases
  • Thinking it only applies to admin database
4. You try to create a user with this command:
db.createUser({user: 'testUser', pwd: 'test123', roles: ['readWrite']})

But it fails. What is the most likely reason?
medium
A. Password is too short
B. User already exists
C. Roles must be objects specifying role and database
D. Missing database name in createUser command

Solution

  1. Step 1: Check roles format in createUser

    Roles must be an array of objects with role and db fields, not just strings.
  2. Step 2: Identify the error cause

    The command uses roles as strings, which is invalid syntax causing failure.
  3. Final Answer:

    Roles must be objects specifying role and database -> Option C
  4. Quick Check:

    Roles = [{role: '', db: ''}] [OK]
Hint: Roles need role and db objects, not strings [OK]
Common Mistakes:
  • Using role names as plain strings
  • Ignoring role database specification
  • Assuming password length causes failure
5. You want to create a custom role named reportViewer that allows users to only read data from the reports collection in the analytics database. Which command correctly creates this role?
hard
A. db.createRole({role: 'reportViewer', privileges: [{resource: {db: 'analytics', collection: 'reports'}, actions: ['read']}], roles: []})
B. db.createRole({role: 'reportViewer', privileges: [{resource: {db: 'analytics'}, actions: ['read']}], roles: []})
C. db.createRole({role: 'reportViewer', privileges: [{resource: {collection: 'reports'}, actions: ['find']}], roles: []})
D. db.createRole({role: 'reportViewer', privileges: [{resource: {db: 'analytics', collection: 'reports'}, actions: ['find']}], roles: []})

Solution

  1. Step 1: Understand custom role creation syntax

    Custom roles require specifying privileges with resource (db and collection) and actions allowed.
  2. Step 2: Analyze each option's privileges

    db.createRole({role: 'reportViewer', privileges: [{resource: {db: 'analytics', collection: 'reports'}, actions: ['find']}], roles: []}) correctly specifies the database 'analytics', collection 'reports', and action 'find' which allows reading documents. db.createRole({role: 'reportViewer', privileges: [{resource: {db: 'analytics'}, actions: ['read']}], roles: []}) uses 'read' action on the whole database, not just the collection. db.createRole({role: 'reportViewer', privileges: [{resource: {collection: 'reports'}, actions: ['find']}], roles: []}) misses the database name. db.createRole({role: 'reportViewer', privileges: [{resource: {db: 'analytics', collection: 'reports'}, actions: ['read']}], roles: []}) uses 'read' action which is not a valid action for privileges; 'find' is correct for reading documents.
  3. Final Answer:

    db.createRole({role: 'reportViewer', privileges: [{resource: {db: 'analytics', collection: 'reports'}, actions: ['find']}], roles: []}) -> Option D
  4. Quick Check:

    Custom role needs db, collection, and 'find' action [OK]
Hint: Use 'find' action for read on specific collection [OK]
Common Mistakes:
  • Using 'read' action instead of 'find'
  • Omitting database name in resource
  • Specifying collection without database
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard