Skip to content

moltinginstar/mongoctl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 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()"

Commands

Profile management

# Create a new profile
mongoctl profile create <name> <uri>

# List all profiles
mongoctl profile list

# Get profile details
mongoctl profile get <name>

# Get only the profile URI, including username and password
mongoctl profile get <name> --field uri --show-credentials
mongoctl profile get <name> -f uri -c

# Delete a profile
mongoctl profile delete <name>

Shell and execution

# Open MongoDB shell
mongoctl shell <profile> [--db <dbname>] [--verbose] [-- <mongosh-flags>]

# Execute MongoDB query
mongoctl exec <profile> "<query>" [--db <dbname>] [--file <file.js>] [--verbose]

Examples

Basic usage

# Create profiles for different environments
mongoctl profile create dev "mongodb://localhost:27017"
mongoctl profile create prod "mongodb+srv://user:pass@cluster.mongodb.net/"

# Shell into dev
mongoctl shell dev

# Execute query against prod
mongoctl exec prod "db.stats()"

# Execute query against specific database
mongoctl exec prod "db.users.find()" --db myapp

Advanced usage

# Verbose mode for debugging
mongoctl shell local --verbose

# Pass flags to mongosh
mongoctl shell local -- --quiet --eval "db.stats()"

# Execute from file
mongoctl exec local --file query.js

# Connect to specific database
mongoctl shell prod --db analytics

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/db"
# Password for user (leave empty to skip): ****

2. Read from stdin (useful for scripts)

echo "$MONGO_PASSWORD" | mongoctl profile create prod "mongodb://user@host/db" --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/db"

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 (in ~/.mongoctl/profiles/) do not contain plaintext passwords.

License

This project is licensed under the MIT License.