MongoDB Mastery: Install, Configure, and Perform CRUD
This comprehensive tutorial walks you through installing MongoDB on Windows, configuring data and log directories, setting environment variables, creating and managing databases, collections, indexes, aggregation pipelines, backup and restore procedures, monitoring tools, advanced query operators, user management, and using a visual tool like Navicat for MongoDB.
Introduction
MongoDB is a distributed document-oriented database written in C++ that provides a scalable, high-performance data storage solution for web applications. It is a popular NoSQL database that can replace traditional relational databases or key/value stores in many scenarios.
1. Installation and Configuration
1.1 Download
Download the installer from the following link: https://590m.com/file/7715018-442253530 Run the MSI installer. It is recommended to install to a non‑C: drive, e.g., E:\mongodb . During installation, avoid selecting the option shown in the second screenshot.
After installation, create the following directories:
# Data directory
E:\mongodb\data\db
# Log directory
E:\mongodb\data\log1.2 Configuration File
Create E:\mongodb\mongo.config with the following content:
# Data file path
dbpath=E:\mongodb\data\db
# Log file path
logpath=E:\mongodb\data\log\mongodb.log
logappend=true
journal=true
quiet=true
port=270171.3 Environment Variable
1.4 Create Database File Location
Start MongoDB with the specified data path:
mongod.exe --dbpath E:\mongodb\data\db1.5 Verify Access
Open a browser and navigate to http://localhost:27017/. If you see a JSON response, MongoDB has started successfully.
1.6 Install as a Service
C:\Users\Administrator>mongod.exe --dbpath E:\mongodb\data\db -logpath E:\mongo\db\log\mongodb.log -install -serviceName "MongoDB"If the service already exists, delete it first:
sc delete MongoDB1.7 Start and Stop MongoDB Service
net start MongoDBnet stop MongoDB2. MongoDB CRUD Operations
MongoDB uses collections instead of tables. Common commands include:
// Create database
use data
// Show databases
show dbs
// Show current database
db
// Drop current database
db.dropDatabase()
// Create collection with size limit
db.createCollection('student',{capped:true,size:100,max:1000})
// Insert document
db.hw.insert({_id:0001,'name':'hw','age':10})
// Find documents
db.hw.find()
// Update document
db.hw.update({'name':'hw'},{'$set':{'name':'xz'}})
// Remove document
db.hw.remove({'name':'hw'},{justOne:true})
// Rename collection
db.user.renameCollection('hw')3. Indexes
// Create unique index
db.hw.ensureIndex({name:1},{unique:true})
// List indexes
db.hw.getIndexes()
// Drop index
db.hw.dropIndex('name')
// Rebuild indexes
db.hw.reIndex()4. Data Aggregation
// Aggregation pipeline example
db.hw.aggregate({$group:{_id:'$name',count:{$sum:1},avg_age:{$avg:'$age'}}},{$project:{name:'$_id',count:'$count',avg_age:'$avg_age'}},{$match:{age:{$gt:20}}},{$unwind:{'$age',preserveNullAndEmptyArrays:true}})5. Backup and Restore
// Backup
mongodump -h dbhost -d dbname -o dbdirectory
// Restore
mongorestore -h dbhost -d dbname --dir dbdirectory6. Monitoring
// Monitor database status
mongostat
mongotop7. Advanced Queries
// Comparison operators
{field:{$gt:value}} // greater than
{field:{$lt:value}} // less than
{field:{$gte:value}} // greater or equal
{field:{$lte:value}} // less or equal
// Logical operators
{$or:[{a:1},{b:2}]}
// Array operators
{colors:'red'}
{$elemMatch:{a:1,b:{$gt:1}}}
// Regex example
{ name:/acme.*corp/i }8. User Management
// Add user
db.addUser('hw')
db.addUser('hw','123321',true) // password, read‑only flag
// Authenticate
db.auth('hw','123123')
// List users
show users
// Remove user
db.removeUser('hw')9. Other Commands
// Storage size of a collection
db.hw.storageSize()
// Total size allocated
db.hw.totalSize()
// Server status
db.serverStatus()
// Database statistics
db.stats()
// MongoDB version
db.version()
// Get connection address
db.getMongo()10. Visual Tool
For a more user‑friendly interface, you can use Navicat for MongoDB. Download link:
https://590m.com/file/7715018-442253555Conclusion
This article covered MongoDB installation, configuration, CRUD operations, index management, aggregation, backup and restore, monitoring, advanced queries, user management, and a visual tool, providing a comprehensive guide to mastering MongoDB.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
