A simple command-line tool for managing MongoDB connection profiles.
- Profile-based connection management: Store MongoDB connection strings as named profiles
- OS keychain integration: Secure password storage
- Quick shell and query execution: Fast access to your MongoDB instances
Make sure mongosh is in your PATH.
brew tap moltinginstar/mongoctl
brew install mongoctlgit clone https://github.com/moltinginstar/mongoctl.git
cd mongoctl
go build -o mongoctl-
Create a profile:
mongoctl profile create local "mongodb://localhost:27017"
For authenticated connections, see the section on secure password input.
-
Open a MongoDB shell:
mongoctl shell local -
Or execute a query directly:
mongoctl exec local "db.users.find()"
# Create profiles for dev and prod
mongoctl profile create dev "mongodb://localhost:27017"
# Including authSource in the URL for use with tools like mongodump/mongorestore
mongoctl profile create prod "mongodb+srv://user:pass@cluster.mongodb.net/?authSource=admin"
# List all profiles
mongoctl profile list
# List profiles as JSON
mongoctl profile list --output json
mongoctl profile list -o json
# Show profile details
mongoctl profile get dev
# Get profile details as JSON
mongoctl profile get dev --output json
mongoctl profile get dev -o json
# Show the full URI (including credentials)
mongoctl profile get prod --field uri --raw
mongoctl profile get prod -f uri -R
# Update the profile URI
mongoctl profile set dev --field uri --value "mongodb://localhost:27017"
mongoctl profile set dev -f uri -x "mongodb://localhost:27017"
# Read new value from stdin
echo "mongodb://localhost:27017" | mongoctl profile set dev --field uri --value-stdin
echo "mongodb://localhost:27017" | mongoctl profile set dev -f uri -X
# Prompt for new URI
mongoctl profile set dev --field uri
mongoctl profile set dev -f uri
# Rename profile
mongoctl profile set prod --field name --value production
mongoctl profile set prod -f name -x production
# Delete a profile
mongoctl profile delete dev
# Open MongoDB shell
mongoctl shell prod
# Open shell with a direct URI
mongoctl shell "mongodb://localhost:27017"
# Connect to specific database
mongoctl shell prod --db analytics
mongoctl shell prod -d analytics
# Pass flags to mongosh
mongoctl shell prod -- --quiet --eval "db.stats()"
# Execute a query using a profile
mongoctl exec prod "db.stats()"
# Execute query against specific database
mongoctl exec prod "db.users.find()" --db myapp
mongoctl exec prod "db.users.find()" -d myapp
# Execute query using a direct URI
mongoctl exec "mongodb://localhost:27017" "db.stats()"
# Execute query from file
mongoctl exec prod --file query.js
mongoctl exec prod -f query.js
# Verbose mode for debugging
mongoctl shell prod --verbose
mongoctl shell prod -vWhen creating profiles with authentication, mongoctl offers three options:
Provide URI with username only - you'll be prompted for the password:
mongoctl profile create prod "mongodb://user@host/authSource=admin"
# Password for user (leave empty to skip): ****echo "$MONGO_PASSWORD" | mongoctl profile create prod "mongodb://user@host/?authSource=admin" --password-stdinConfigure your shell to ignore commands starting with space:
# In ~/.bashrc or ~/.zshrc
export HISTCONTROL=ignorespace # bash
setopt HIST_IGNORE_SPACE # zsh
# Then prefix command with space to avoid history
mongoctl profile create prod "mongodb://user:pass@host/?authSource=admin"This will hide the password from your shell history but not from process lists.
Note: Passwords are automatically extracted and stored in your OS keychain. Profiles (stored in ~/.mongoctl/profiles/) do not contain plaintext passwords.
This project is licensed under the MIT License.