Common Shell Commands
Database Management Commands
Listing Databases
## Show all databases
show dbs
## Show current database
db
Switching Databases
## Switch to a specific database
use myDatabase
Collection Operations
Collection Management
Command |
Description |
Example |
show collections |
List all collections |
|
db.createCollection() |
Create new collection |
db.createCollection("users") |
db.collection.drop() |
Delete a collection |
db.users.drop() |
Data Manipulation Commands
Insertion
## Insert a single document
db.users.insertOne({
name: "John Doe",
age: 30,
email: "[email protected]"
})
## Insert multiple documents
db.users.insertMany([
{name: "Alice", age: 25},
{name: "Bob", age: 35}
])
Querying Data
## Find all documents
db.users.find()
## Find with specific condition
db.users.find({age: {$gt: 25}})
Advanced Shell Commands
graph TD
A[Shell Commands] --> B[Database Operations]
A --> C[Collection Management]
A --> D[Data Manipulation]
A --> E[Administrative Tasks]
Administrative Commands
## Check server status
db.serverStatus()
## Get current connection
db.runCommand({connectionStatus: 1})
## Analyze query performance
db.users.find({age: 30}).explain("executionStats")
Shell Configuration
Helpful Configuration Commands
## Set shell prompt
prompt = function() {
return db + "> ";
}
## Configure display settings
DBQuery.shellBatchSize = 10
By mastering these common MongoDB Shell commands, users can efficiently manage databases, manipulate data, and perform administrative tasks in their LabEx MongoDB environment.