Lecture Notes Of Day 24:
MongoDB Administration (Basic)
Objective:
By the end of this class,
students will be familiar with basic MongoDB administrative tasks. They will
learn how to manage databases, collections, and monitor MongoDB performance
using simple commands.
Outcome:
Students will be able to:
- Create,
view, and delete databases.
- Perform
basic operations on collections.
- Monitor
database health and performance.
- Understand
MongoDB logs and configuration.
Introduction
to MongoDB Administration
MongoDB is a NoSQL database known
for its high performance and scalability. As a database administrator (DBA),
you will need to perform various administrative tasks such as managing
databases, collections, monitoring performance, and troubleshooting.
1. Basic
MongoDB Commands
MongoDB provides a variety of
commands for managing databases and collections. Let's begin by discussing the
fundamental commands used in MongoDB.
a.
Connecting to MongoDB:
Before performing any
administrative tasks, you need to connect to MongoDB. Open the MongoDB shell by
running the following command in your terminal:
bash
CopyEdit
mongo
This command connects to the
default MongoDB server running on localhost with the default port 27017.
b. Show
Databases:
To list all the databases in
MongoDB, use the following command:
js
CopyEdit
show
databases
This will return a list of all
available databases on the server.
c. Create
a Database:
In MongoDB, databases are created
automatically when you insert data into them. However, you can explicitly
create a database by using the use command:
js
CopyEdit
use
<database_name>
This command will switch to the
specified database. If the database doesn't exist, it will be created as soon
as you insert data into it.
d. Delete
a Database:
To delete a database, first,
switch to the database you want to delete using the use command, and then run:
js
CopyEdit
db.dropDatabase()
This will delete the database and
all its collections.
2.
Managing Collections in MongoDB
Collections are groups of
documents in MongoDB. MongoDB is a schema-less database, which means that
collections can store documents with different structures.
a. Show
Collections:
To view the collections in the
current database, use the following command:
js
CopyEdit
show
collections
b. Create
a Collection:
MongoDB automatically creates
collections when you insert a document. However, if you need to create an empty
collection, you can use:
js
CopyEdit
db.createCollection("<collection_name>")
This creates a new, empty
collection.
c. Drop a
Collection:
To delete a collection and all
its documents, use the drop command:
js
CopyEdit
db.<collection_name>.drop()
3.
Inserting, Querying, and Updating Documents
Now that you know how to manage
databases and collections, let’s review the basic document operations.
a. Insert
Documents:
You can insert documents into a
collection using the insertOne or insertMany commands.
- Insert
One Document:
js
CopyEdit
db.<collection_name>.insertOne({
key: value })
- Insert
Multiple Documents:
js
CopyEdit
db.<collection_name>.insertMany([{
key: value }, { key: value }])
b. Query
Documents:
To query documents in a
collection, use the find command. The find method returns all documents in the
collection unless a query filter is specified.
- Find
All Documents:
js
CopyEdit
db.<collection_name>.find()
- Find
Documents with a Condition:
js
CopyEdit
db.<collection_name>.find({
key: value })
c. Update
Documents:
You can update documents in a
collection using the updateOne or updateMany commands.
- Update
One Document:
js
CopyEdit
db.<collection_name>.updateOne(
{ query_filter },
{ $set: { field_to_update: new_value } }
)
- Update
Multiple Documents:
js
CopyEdit
db.<collection_name>.updateMany(
{ query_filter },
{ $set: { field_to_update: new_value } }
)
4.
Monitoring MongoDB Performance
MongoDB provides various tools to
help you monitor database performance and health. Some of the key
administrative commands include:
a.
Checking Server Status:
To view the current status of
your MongoDB server, use the following command:
js
CopyEdit
db.serverStatus()
This command returns detailed
information about memory usage, connections, network operations, and more.
b.
Checking Database Stats:
To check statistics for the
current database, including the number of collections, objects, and storage
size, use:
js
CopyEdit
db.stats()
c.
Checking Collection Stats:
To view statistics for a
particular collection, including the number of documents and storage usage,
use:
js
CopyEdit
db.<collection_name>.stats()
5.
Understanding MongoDB Logs
MongoDB keeps logs of various
events, errors, and operational information. These logs are crucial for
diagnosing issues and monitoring the health of the system.
- Location
of Logs: By default, MongoDB logs are stored in the logpath
specified in the MongoDB configuration file (mongod.conf). The default log
file is typically located in /var/log/mongodb/mongod.log for Linux-based
systems.
- Checking
Logs: You can access MongoDB logs using
command-line tools like tail:
bash
CopyEdit
tail -f
/var/log/mongodb/mongod.log
This command will display the
most recent entries in the MongoDB log file in real-time.
6.
MongoDB Configuration File
MongoDB's behavior is governed by
a configuration file (mongod.conf). You can modify this file to configure
various settings such as:
- Port
number
- Data
storage location
- Log
file location
- Network
settings
- Replica
sets and sharding (for advanced configurations)
7. Basic
MongoDB Backup and Restore
It's important to regularly back
up your MongoDB databases. MongoDB provides built-in tools for backup and
restore operations.
- Backup
Database:
bash
CopyEdit
mongodump
--db <database_name> --out <backup_directory>
- Restore
Database:
bash
CopyEdit
mongorestore
--db <database_name> <backup_directory>/<database_name>
8.
Conclusion
In this class, we have covered
the essential MongoDB administration tasks, including creating and managing
databases and collections, querying and updating documents, monitoring server
performance, and basic backup and restore procedures. Understanding these basic
administrative functions is key to effectively managing MongoDB in a production
environment.
Assignment:
1. Create a
database named library.
2. Create
two collections: books and authors.
3. Insert at
least three documents into each collection.
4. Query and
update documents in both collections.
5. Use the db.stats()
and db.<collection_name>.stats() commands to monitor your database and
collection statistics.
Further
Reading:
- MongoDB
Documentation: MongoDB Admin Commands
- MongoDB
Monitoring and Performance: MongoDB Performance Best Practices
