mongosh

Crates.iomongosh
lib.rsmongosh
version0.7.0
created_at2025-12-08 06:21:57.665183+00
updated_at2026-01-12 07:03:23.501004+00
descriptionA high-performance MongoDB Shell implementation in Rust
homepage
repositoryhttps://github.com/daleione/mongosh
max_upload_size
id1972805
size813,761
Dalei (daleione)

documentation

README

Rust MongoDB Power CLI

Crates.io Rust License: MIT

A powerful MongoDB CLI written in Rust, featuring intelligent auto-completion, SQL query support, and enhanced security features for productive database operations.

Note: This project is an independent, community-driven tool. It is NOT affiliated with MongoDB, and it is not intended to be a drop-in replacement for the official mongosh.

๐Ÿ” Key Differences vs Official mongosh

Feature Official mongosh This Project
Implementation Node.js Rust (async)
JS Runtime Full JavaScript โŒ Not a JS shell
Startup Time Slower Fast
Output JSON-first Tables + highlighted JSON
Scripting JS-based CLI / batch-oriented
Target Users General users Power users / DevOps

โœจ Features

  • โšก High Performance โ€” Native Rust, async I/O
  • ๐Ÿ’พ Lightweight โ€” Small static binary
  • ๐ŸŽจ Syntax Highlighting โ€” Readable command & JSON output
  • ๐Ÿ“Š Rich Output Formats โ€” JSON (pretty/compact), shell-style, and table views
  • ๐Ÿ—„๏ธ SQL Query Support โ€” Query MongoDB using familiar SQL SELECT syntax
  • ๐Ÿง  Intelligent Auto-Completion โ€” Context-aware suggestions for MongoDB shell and SQL commands

๐Ÿ“ฆ Installation

cargo install mongosh

Note: The binary name may change in the future to avoid conflicts with the official MongoDB shell.


๐Ÿš€ Quick Start

Connect to MongoDB

# Connect to local MongoDB
mongosh

# Connect to a specific host
mongosh mongodb://localhost:27017

# Connect with authentication (credentials are automatically sanitized in logs)
mongosh mongodb://username:password@localhost:27017/dbname

๐Ÿงช Example Commands

// Show Databases
show dbs

// Switch Database
use mydb

// Show Collections
show collections

// Insert a Document
db.users.insertOne({ name: "John Doe", age: 25 });

// Query Documents
db.users.find({ age: { $gte: 18 } });

// Update Documents
db.users.updateOne({ name: "John Doe" }, { $set: { age: 26 } });

// Aggregation Pipeline
db.orders.aggregate([
  { $match: { status: "completed" } },
  { $group: { _id: "$userId", total: { $sum: "$amount" } } },
]);

๐Ÿ” SQL Query Support

This shell now supports SQL SELECT queries that are automatically translated to MongoDB queries!

Basic SELECT Queries

-- Simple query with filtering and sorting
SELECT name, age FROM users WHERE age > 18 ORDER BY name ASC

-- Pagination with LIMIT and OFFSET
SELECT * FROM users LIMIT 10 OFFSET 5

Aggregate Functions

-- Column aliases support both identifiers and quoted strings
SELECT group_id AS 'group_id', COUNT(*) FROM templates GROUP BY group_id

-- Group by with multiple aggregates
SELECT
  category,
  COUNT(*) AS total,
  SUM(price) AS revenue
FROM products
GROUP BY category

Supported SQL Features

  • โœ… SELECT with column list or *
  • โœ… FROM clause
  • โœ… WHERE with comparison operators (=, !=, >, <, >=, <=)
  • โœ… Logical operators (AND, OR)
  • โœ… GROUP BY with aggregation functions (COUNT, SUM, AVG, MIN, MAX)
  • โœ… ORDER BY with ASC/DESC
  • โœ… LIMIT and OFFSET
  • โœ… Column aliases with AS (supports both identifiers and string literals)

SQL to MongoDB Translation Examples

SQL MongoDB
SELECT * FROM users db.users.find({})
SELECT name, age FROM users db.users.find({}, {name: 1, age: 1})
WHERE age > 18 {age: {$gt: 18}}
WHERE status = 'active' AND age >= 18 {$and: [{status: 'active'}, {age: {$gte: 18}}]}
ORDER BY name ASC {name: 1}
LIMIT 10 limit(10)
GROUP BY category aggregate([{$group: {_id: "$category"}}])
SELECT COUNT(*) FROM users aggregate([{$group: {_id: null, COUNT_*: {$sum: 1}}}])

Notes

  • SQL queries are automatically detected when starting with SELECT
  • Complex JOIN operations are not yet supported
  • Subqueries are not yet supported

๐Ÿ“„ License

Licensed under the MIT License.


๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

๐Ÿ“ฌ Feedback

If you have any questions, suggestions, or issues, please open an issue on GitHub.

Commit count: 91

cargo fmt