Bird
Raised Fist0
MongoDBquery~10 mins

Authentication mechanisms in MongoDB - Interactive Code Practice

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

Complete the code to specify the authentication mechanism when connecting to MongoDB.

MongoDB
mongo --username user --password pass --authenticationMechanism [1]
Drag options to blanks, or click blank then click option'
ANoAuth
BSCRAM-SHA-256
COAuth
DLDAP
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'NoAuth' which disables authentication and is insecure.
Confusing LDAP with MongoDB's default authentication.
2fill in blank
medium

Complete the code to create a user with SCRAM-SHA-1 authentication mechanism in MongoDB.

MongoDB
db.createUser({user: 'alice', pwd: 'secret', roles: ['readWrite'], [1]: ["SCRAM-SHA-1"]})
Drag options to blanks, or click blank then click option'
AauthMechanism
Bmechanism
Cmechanisms
DauthMethod
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'authMechanism' which is not recognized in this context.
Using 'mechanism' or 'authMethod' which are invalid keys.
3fill in blank
hard

Fix the error in the connection string to use the correct authentication mechanism.

MongoDB
mongodb://user:pass@host:27017/db?authMechanism=[1]
Drag options to blanks, or click blank then click option'
APLAIN
BMONGODB-X509
CSCRAM-SHA-1
DSCRAM-SHA-256
Attempts:
3 left
💡 Hint
Common Mistakes
Using SCRAM-SHA-1 which is less secure.
Using PLAIN without TLS which is insecure.
4fill in blank
hard

Fill both blanks to configure MongoDB to use X.509 certificate authentication.

MongoDB
mongo --host [1] --ssl --sslPEMKeyFile [2] --authenticationMechanism MONGODB-X509
Drag options to blanks, or click blank then click option'
Acluster0.mongodb.net
B/etc/ssl/mongodb.pem
Clocalhost
D/home/user/key.pem
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'localhost' as host when connecting to a remote cluster.
Using a wrong or missing PEM key file path.
5fill in blank
hard

Fill all three blanks to create a user with LDAP authentication and assign the read role.

MongoDB
db.createUser({user: '[1]', roles: ['[2]'], [3]: ["PLAIN"]})
Drag options to blanks, or click blank then click option'
AldapUser
Bread
Cmechanisms
Dwrite
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'write' role instead of 'read'.
Using wrong key for authentication mechanism.

Practice

(1/5)
1. What is the main purpose of authentication in MongoDB?
easy
A. To create indexes on collections
B. To backup the database automatically
C. To optimize query performance
D. To verify the identity of a user before granting access

Solution

  1. Step 1: Understand authentication concept

    Authentication means checking who you are before allowing access.
  2. Step 2: Identify the purpose in MongoDB

    MongoDB uses authentication to verify user identity to protect data.
  3. Final Answer:

    To verify the identity of a user before granting access -> Option D
  4. Quick Check:

    Authentication = Verify identity [OK]
Hint: Authentication means checking who you are [OK]
Common Mistakes:
  • Confusing authentication with backup
  • Thinking authentication improves query speed
  • Mixing authentication with indexing
2. Which of the following is the correct syntax to authenticate a user in MongoDB shell?
easy
A. db.auth('username', 'password')
B. db.authenticate('username', 'password')
C. db.login('username', 'password')
D. db.userAuth('username', 'password')

Solution

  1. Step 1: Recall MongoDB authentication method

    The correct method to authenticate is db.auth().
  2. Step 2: Match syntax with options

    Only db.auth('username', 'password') matches the correct syntax.
  3. Final Answer:

    db.auth('username', 'password') -> Option A
  4. Quick Check:

    Authenticate = db.auth() [OK]
Hint: Use db.auth() to log in with username and password [OK]
Common Mistakes:
  • Using db.authenticate() which does not exist
  • Using db.login() which is invalid
  • Confusing method names with userAuth()
3. Consider this MongoDB shell command sequence:
use admin
var result = db.auth('alice', 'mypassword')
What will be the value of result if authentication succeeds?
medium
A. 1
B. false
C. true
D. null

Solution

  1. Step 1: Understand db.auth() return value

    When authentication succeeds, db.auth() returns 1.
  2. Step 2: Match return value with options

    Only 1 matches the expected numeric return value.
  3. Final Answer:

    1 -> Option A
  4. Quick Check:

    Success returns 1 [OK]
Hint: db.auth() returns 1 if login works [OK]
Common Mistakes:
  • Expecting boolean true instead of numeric 1
  • Thinking it returns false on success
  • Assuming it returns null or undefined
4. You run db.auth('bob', 'wrongpass') but it always returns 1. What is the most likely cause?
medium
A. The user 'bob' does not exist
B. The password is case-insensitive
C. You are already authenticated as 'bob' or another user
D. MongoDB does not check passwords

Solution

  1. Step 1: Understand db.auth() behavior when already logged in

    If already authenticated, db.auth() returns 1 without rechecking password.
  2. Step 2: Analyze why wrong password still returns 1

    This happens because the session is already authenticated as 'bob' or another user.
  3. Final Answer:

    You are already authenticated as 'bob' or another user -> Option C
  4. Quick Check:

    Already logged in means db.auth() returns 1 [OK]
Hint: db.auth() returns 1 if already logged in [OK]
Common Mistakes:
  • Assuming wrong password always returns false
  • Thinking MongoDB ignores passwords
  • Believing user must exist for true return
5. You want to create a user with read-only access to the sales database. Which command correctly creates this user with proper authentication roles?
hard
A. db.addUser('readonly', 'pass123', 'read')
B. db.createUser({user: 'readonly', pwd: 'pass123', roles: [{role: 'read', db: 'sales'}]})
C. db.createUser({username: 'readonly', password: 'pass123', roles: ['read']})
D. db.createUser({user: 'readonly', pwd: 'pass123', roles: ['readWrite']})

Solution

  1. Step 1: Identify correct syntax for creating user with roles

    The correct syntax uses db.createUser() with user, pwd, and roles as an array of objects specifying role and database.
  2. Step 2: Match read-only role on 'sales' database

    db.createUser({user: 'readonly', pwd: 'pass123', roles: [{role: 'read', db: 'sales'}]}) correctly assigns role 'read' on database 'sales'.
  3. Final Answer:

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

    Create user with roles array = db.createUser({user: 'readonly', pwd: 'pass123', roles: [{role: 'read', db: 'sales'}]}) [OK]
Hint: Use db.createUser() with roles array for access control [OK]
Common Mistakes:
  • Using deprecated db.addUser() method
  • Incorrect field names like username instead of user
  • Assigning wrong roles like readWrite for read-only access
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