Skip to content

moltinginstar/mongoctl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mongoctl

License: MIT

A simple command-line tool for managing MongoDB connection profiles.

Features

  • 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

Installation

Make sure mongosh is in your PATH.

Using Homebrew

brew tap moltinginstar/mongoctl
brew install mongoctl

From source

git clone https://github.com/moltinginstar/mongoctl.git
cd mongoctl
go build -o mongoctl

Quick start

  1. Create a profile:

    mongoctl profile create local "mongodb://localhost:27017"

    For authenticated connections, see the section on secure password input.

  2. Open a MongoDB shell:

    mongoctl shell local
  3. Or execute a query directly:

    mongoctl exec local "db.users.find()"

Usage

# 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 -v

Secure password input

When creating profiles with authentication, mongoctl offers three options:

1. Interactive prompt (recommended)

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): ****

2. Read from stdin (useful for scripts)

echo "$MONGO_PASSWORD" | mongoctl profile create prod "mongodb://user@host/?authSource=admin" --password-stdin

3. Inline with HIST_IGNORE_SPACE (not recommended)

Configure 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.

License

This project is licensed under the MIT License.