First Shell Commands
Database Operations
Creating and Switching Databases
## Create/Switch to a new database
use labexDatabase
## Check current database
db
Listing Databases
## Show all databases
show dbs
Collection Management
Creating Collections
## Create a new collection
## Insert document into collection
CRUD Operations
graph TD
A[CRUD Operations] --> B[Create]
A --> C[Read]
A --> D[Update]
A --> E[Delete]
Insert Operations
## Insert single document
## Insert multiple documents
Query Operations
## Find all documents
## Find specific document
## Find with conditions
Query Operators
| Operator |
Description |
Example |
| $eq |
Equal to |
{age: {$eq: 30}} |
| $gt |
Greater than |
{age: {$gt: 25}} |
| $lt |
Less than |
{age: {$lt: 40}} |
| $and |
Logical AND |
{and: [{age: {gt: 25}}, {role: "developer"}]} |
Update and Delete
Update Documents
## Update single document
## Update multiple documents
Delete Documents
## Delete single document
## Delete multiple documents
Advanced Shell Commands
Aggregation
db.users.aggregate([
{$group: {_id: "$role", total: {$sum: 1}}}
])
Shell Configuration in LabEx
When using LabEx platform, the MongoDB shell provides an intuitive environment for learning and practicing database operations, with pre-configured settings for seamless interaction.
Best Practices
- Always use filters in queries
- Limit result sets
- Create appropriate indexes
- Use projection to return specific fields