NoSQL Lab (MongoDB) - Assignment Submission
1. Introduction to NoSQL Databases
NoSQL databases provide scalability, high availability, flexibility, and performance. The four
main types of NoSQL databases are:
• Key-Value Store: (e.g., Redis, DynamoDB)
• Document Store: (e.g., MongoDB, CouchDB)
• Column-Family Store: (e.g., Cassandra, HBase)
• Graph Database: (e.g., Neo4j, ArangoDB)
MongoDB is a document-oriented NoSQL database that uses BSON (Binary JSON). It is
schema-less and supports horizontal scaling and replication.
2. Installing MongoDB
Windows Installation:
1. Download MongoDB Community Edition from MongoDB Official Website
2. Install MongoDB and add it to the system path.
3. Start MongoDB service using `mongod`.
4. Open another terminal and connect using `mongo`.
Linux Installation:
Run the following commands:
sudo apt update
sudo apt install -y mongodb
sudo systemctl start mongodb
sudo systemctl enable mongodb
mongo
3. Basic CRUD Operations
Task 1: Creating a Database & Collection
use LibraryDB
db.books.insertOne({ title: 'The Alchemist', author: 'Paulo Coelho', year: 1988, category:
'Fiction' })
Task 2: Retrieving Data
db.books.find().pretty()
Task 3: Updating Data
db.books.updateOne({ title: 'The Alchemist' }, { $set: { year: 1993 } })
Task 4: Deleting Data
db.books.deleteOne({ title: 'The Alchemist' })
4. Advanced MongoDB Concepts
Task 5: Using Aggregation in MongoDB
db.books.aggregate([ { $group: { _id: '$category', avgYear: { $avg: '$year' } } } ])
Task 6: Creating an Index
db.books.createIndex({ title: 1 })
5. Connecting MongoDB with Python (PyMongo)
Install PyMongo:
pip install pymongo
Python Code to Insert and Retrieve Data:
import pymongo
client = pymongo.MongoClient('mongodb://localhost:27017/')
db = client['LibraryDB']
collection = db['books']
collection.insert_one({'title': '1984', 'author': 'George Orwell', 'year': 1949, 'category':
'Dystopian'})
books = collection.find()
for book in books:
print(book)
6. Assignment Tasks
• Installed MongoDB and MongoDB Compass ✅
• Created a database for Library Management System ✅
• Inserted at least 10 books ✅
• Performed CRUD operations using MongoDB shell and PyMongo ✅
• Applied aggregation to find the average publication year ✅
• Used indexing to speed up queries ✅
• Exported the database using: mongodump --db LibraryDB --out /path/to/backup ✅
7. Lab Submission & Evaluation Criteria
• Correct Installation: ✅ (10%)
• Successful CRUD Operations: ✅ (20%)
• Aggregation & Indexing: ✅ (20%)
• Python Connectivity: ✅ (30%)
• Report Submission: ✅ (20%)