Skip to content

Instantly share code, notes, and snippets.

@eliskvitka
Created September 29, 2021 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliskvitka/ea244a1e30d8c7ca7309135c9a61e12b to your computer and use it in GitHub Desktop.
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
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