2

I'm not a Bash/Linux person, but I've been asked to just come up with the lines needed to run to authenticate against a database and then drop it. Here is what I have so far and I'm not sure if it's correct:

mongo mydatabase –port –username <username> –password <password> –authenticationDatabase admin –eval “ printjson(db.dropDatabase())”

If it's possible to use one line to delete any database whose name starts with "cpt_" then that would be absolutely ideal.

Thanks!

1 Answer 1

3

You can use -

  • Get all the names
  • Loop and check if the name starts with cpt_
  • drop the database

db.getMongo().getDBNames().forEach(function(dbName){ if (dbName.startsWith('cpt_')) printjson(db.getSiblingDB(dbName).dropDatabase())

Full

mongo –port –username <username> –password <password> –authenticationDatabase admin --eval "db.getMongo().getDBNames().forEach(function(dbName){ if (dbName.startsWith('cpt_')) printjson(db.getSiblingDB(dbName).dropDatabase())  })"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.