Saturday, 22 May 2021

How to drop the single user and all the users in MongoDB? | MongoDB Tutorial for Beginners

🚀 Loved the tutorial? Join the Ram N Java community!

Subscribe to stay updated with the simplest tech guides on the web.

🔔 SUBSCRIBE TO OUR CHANNEL

How to Easily Drop Users in MongoDB

Managing users is a critical part of database administration. Sometimes you need to remove a single user, and other times you might need to clear out everyone. In this guide, we'll show you exactly how to do both using simple commands!

1. Dropping a Single User

If you want to remove just one specific person from your database, MongoDB provides a dedicated command for that. It’s quick and effective.

db.dropUser("username")

Simply replace "username" with the actual name of the user you want to delete. Make sure you are in the correct database where the user was created!

2. Removing All Users at Once

Need to start fresh? You can remove every single user associated with the current database using the dropAllUsersFromDatabase command.

db.dropAllUsersFromDatabase()

⚠️ Caution: This action cannot be undone! Ensure you have a backup or a way to recreate admin users if necessary before running this command.

Step-by-Step Summary for Beginners

  • Step 1: Open your MongoDB shell or compass terminal.
  • Step 2: Switch to the database where the user exists using use dbName.
  • Step 3: Run the drop command that fits your needs.
  • Step 4: Verify the user is gone by running db.getUsers().

Pro Tip

Always double-check which database you are currently using. Running a drop command in the wrong database can lead to unexpected permission issues!

More from Ram N Java:

No comments:

Post a Comment

Tutorials