Created
September 29, 2021 08:02
-
-
Save eliskvitka/ea244a1e30d8c7ca7309135c9a61e12b to your computer and use it in GitHub Desktop.
[mongodb] Delete user from system collection if user database doesn't exist. Use it from mongoshell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var users = db.system.users.distinct("_id") | |
var dbs = db.getMongo().getDBNames() | |
users.forEach( function(uid) { | |
var dbname = uid.split(".")[0] | |
if (dbs.includes(dbname)) { | |
print(dbname + " exist") | |
} else { | |
db.getMongo().getDB(dbname).dropUser("dev_user") | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment