Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
[mongo db_exists command] MongoDB command to check if a database exists. I use it with a puppet script to conditionally load up a database dump if one isn't detected. Useful for provisioning Vagrant. Returns with exit code zero if the database was found. #mongodb
function db_exists(db_name) {
db = db.getSiblingDB('admin');
if ( == db_name) {
// quit with exit code zero if we found our db
// quit with exit code 1 if db was not found
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment