MongoDB โ MongoDB Compass & mongosh CLI
Master the visual features of MongoDB Compass and the command-line REPL environment of mongosh. Learn how to run scripts, visualize indexes, and inspect schema structures.
1MongoDB Compass Features Overview
MongoDB Compass is the official graphical user interface (GUI). It allows developers and DBAs to visually query, analyze, and optimize MongoDB data without typing raw commands.
- Documents Tab: View records in Tree View, JSON View, or Table View. Edit or delete documents with single-click buttons.
- Schema Tab: Analyze field types, sampling data to inspect data cleanliness and missing fields.
- Explain Plan Tab: Visually inspect query execution stages (IXSCAN vs COLLSCAN).
- Aggregations Pipeline Builder: Build multi-stage aggregation pipelines with instant intermediate stage previews.
2Mastering the mongosh Interactive REPL
mongosh is a modern Node.js-based REPL environment that supports ES6 JavaScript syntax, asynchronous promises, and custom helper methods.
Useful mongosh Shortcuts & Commands
// List databases
show dbs
// Switch database context
use company_db
// Show active database name
db
// List collections
show collections
// Run inline JavaScript loops in mongosh
for (let i = 1; i <= 5; i++) {
db.logs.insertOne({ logId: i, timestamp: new Date() });
}
// Print collection statistics
db.logs.stats()
3Visual vs CLI Workflow Comparison
| Task | mongosh CLI | MongoDB Compass GUI |
|---|---|---|
| Quick CRUD Testing | โก Extremely fast with command history | Good for visual visual editing |
| Building Aggregation Pipeline | Requires manual JSON array syntax | โจ Visual stage-by-stage pipeline editor |
| Server Admin & Scripting | Automation scripts via .js files | Read-only inspection |
| Schema Analysis | Requires aggregations / mapReduce | Visual charts showing data types |