mongodb

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 21, 2025 License: MIT Imports: 10 Imported by: 0

README

MongoDB CLI

Go Reference Go Report Card

A standalone, feature-rich MongoDB interactive CLI client for Go applications.

Features

  • 🚀 Full MongoDB protocol support
  • 🔧 Customizable connection parameters
  • 🔒 TLS/SSL support
  • 💾 Connection pooling
  • 📊 Pretty print JSON output
  • ⏱️ Query timing
  • 🔄 Replica set support
  • 🎯 Database and collection operations
  • 👤 Authentication with configurable auth source

Installation

go get github.com/binrchq/mongodb-cli

Quick Start

package main

import (
    "log"
    "os"
    
    mongodbcli "github.com/binrchq/mongodb-cli"
)

func main() {
    cli := mongodbcli.NewCLI(
        os.Stdin,
        "localhost",
        27017,
        "admin",
        "password",
        "mydb",
    )
    
    if err := cli.Connect(); err != nil {
        log.Fatal(err)
    }
    defer cli.Close()
    
    if err := cli.Start(); err != nil {
        log.Fatal(err)
    }
}

Advanced Configuration

config := &mongodbcli.Config{
    Host:                   "localhost",
    Port:                   27017,
    Username:               "admin",
    Password:               "password",
    Database:               "mydb",
    AuthSource:             "admin",
    ReplicaSet:             "rs0",
    ConnectTimeout:         15 * time.Second,
    ServerSelectionTimeout: 15 * time.Second,
    MaxPoolSize:            200,
    MinPoolSize:            10,
    RetryWrites:            true,
    RetryReads:             true,
    TLS:                    true,
}

cli := mongodbcli.NewCLIWithConfig(terminal, config)

Database Commands

  • show dbs - List databases
  • use <db> - Switch database
  • show collections - List collections
  • show users - List users

Collection Operations

db.users.find()
db.users.find({"age": 25})
db.users.findOne({"name": "John"})
db.users.insertOne({"name": "Alice", "age": 30})
db.users.insertMany([{...}, {...}])
db.users.deleteOne({"name": "Alice"})
db.users.deleteMany({"age": {"$gt": 50}})
db.users.count()
db.users.count({"status": "active"})

Special Commands

  • help - Show help
  • exit, quit - Exit
  • pretty - Toggle pretty print
  • timing - Toggle timing

Requirements

  • Go 1.21 or higher
  • MongoDB 4.0 or higher

Dependencies

License

MIT License - see LICENSE file for details.

Author

Maintained by binrc.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CLI

type CLI struct {
	// contains filtered or unexported fields
}

CLI MongoDB 交互式命令行客户端

func NewCLI

func NewCLI(term Terminal, host string, port int, username, password, database string) *CLI

NewCLI 创建新的 MongoDB CLI 实例(兼容旧接口)

func NewCLIWithConfig

func NewCLIWithConfig(term Terminal, config *Config) *CLI

NewCLIWithConfig 使用配置创建 MongoDB CLI 实例

func (*CLI) Close

func (c *CLI) Close() error

Close 关闭 MongoDB 连接

func (*CLI) Connect

func (c *CLI) Connect() error

Connect 连接到 MongoDB

func (*CLI) Start

func (c *CLI) Start() error

Start 启动交互式命令行

type Config

type Config struct {
	Host                   string
	Port                   int
	Username               string
	Password               string
	Database               string
	AuthSource             string        // 认证数据库,默认 admin
	ReplicaSet             string        // 副本集名称
	ConnectTimeout         time.Duration // 连接超时,默认 10s
	ServerSelectionTimeout time.Duration // 服务器选择超时,默认 10s
	SocketTimeout          time.Duration // Socket 超时,默认 30s
	MaxPoolSize            int           // 最大连接池大小,默认 100
	MinPoolSize            int           // 最小连接池大小,默认 0
	MaxConnIdleTime        time.Duration // 最大连接空闲时间,默认 0
	RetryWrites            bool          // 是否重试写入,默认 true
	RetryReads             bool          // 是否重试读取,默认 true
	Direct                 bool          // 是否直接连接,默认 false
	TLS                    bool          // 是否启用 TLS,默认 false
	CustomParams           string        // 自定义参数,如 "param1=value1&param2=value2"
}

Config MongoDB 连接配置

type ReadWriteCloser added in v0.1.1

type ReadWriteCloser struct {
	io.ReadWriter
}

ReadWriteCloser wraps io.ReadWriter to add a no-op Close method

func (*ReadWriteCloser) Close added in v0.1.1

func (rwc *ReadWriteCloser) Close() error

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader 从终端读取输入(使用 readline 以支持SSH session)

func NewReader

func NewReader(term io.ReadWriter) *Reader

NewReader 创建新的 Reader

func (*Reader) Close

func (r *Reader) Close() error

Close 关闭读取器

func (*Reader) ReadLine

func (r *Reader) ReadLine() (string, error)

ReadLine 读取一行输入

type ServerInfo

type ServerInfo struct {
	Version      string
	GitVersion   string
	Architecture string
	MaxBsonSize  int32
	Uptime       float64
	Connections  int32
}

ServerInfo MongoDB 服务器信息

type Terminal

type Terminal interface {
	io.Reader
	io.Writer
}

Terminal 终端接口,用于输入输出

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL