Big Data Practical 3
Big Data Practical 3
Theory:
MongoDB is a NoSQL, document-oriented database that stores data in JSON-like format, making it suitable for
handling Big Data. Unlike traditional relational databases, MongoDB does not rely on tables and schemas, allowing
for flexible data structures. It supports horizontal scaling, which is crucial for managing and analyzing vast datasets
efficiently in Big Data environments.
Big Data refers to datasets that are too large or complex for traditional data-processing software. These datasets
require specific technologies, such as MongoDB, for effective storage, retrieval, and processing. MongoDB’s
distributed architecture and flexible schema design make it an excellent tool for managing the velocity, variety, and
volume associated with Big Data.
Advantages of MongoDB:
Flexibility: MongoDB's schema-less structure allows for flexibility in data modeling.
Horizontal Scalability: Can handle large datasets by scaling out across servers.
Rich Query Language: Supports advanced queries, indexing, and aggregations.
Disadvantages of MongoDB:
Lack of Transactions: MongoDB was traditionally limited in multi-document transactions (though
newer versions support it).
Memory Usage: MongoDB may consume more memory compared to relational databases due to its
design.
Less Suitable for Complex Queries: It can be less efficient for complex, multi-table joins.
a) Creating a Database:
Databases in MongoDB are created implicitly when data is inserted into a collection. You can switch to a
database using the use command.
Example: use big_data
b) Creating Collections:
Collections are analogous to tables in relational databases. Collections in MongoDB can be created using the
db.createCollection() method or automatically when inserting a document.
Example: db.createCollection("employees")
db.createCollection("Student")
db.createCollection("big_data")
Show Collections
c) DROP COLLECTION:
The drop() Method MongoDB's db.collection.drop() is used to drop a collection from the database.
Example: db.employees.drop()
d) DROP DATABASE:
The dropDatabase() Method MongoDB db.dropDatabase() command is used to drop a existing database.
Example: db.dropDatabase()
CRUD DOCUMENT:
a) Create Operations:
Insert data into a collection using insertOne() for a single document or insertMany() for multiple
documents.
Method Description
It is used to insert a single document in the
db.collection.insertOne()
collection.
b) Read Operations:
Query data from the collection using find() or findOne() to retrieve all documents or a specific
document, respectively.
c) Update Operations:
Modify existing documents using updateOne() or updateMany(). MongoDB allows for targeted
updates using filters.
d) Delete Operations:
Remove documents from the collection using deleteOne() or deleteMany().
Code:
show dbs
use big_data
db.createCollection("RecordsDB")
InsertOne()
db.RecordDB.insertOne({ name: "Bhairi", age: "24 Years", Batch: "A", Roll_No: "8" })
InsertMany()
db.RecordDB.insertMany({ name: "Bhairavi", age: "23 Years", Batch: "B", Roll_No: "5"},
{ name: "Gugle", age: "20 Years", Batch: "C", Roll_No: "1"})
Find():
db.RecordDB.find()
FindOne():
db.RecordDB.find({"Batch":"C"})
db.RecordDB.find({"age":"24 Years"})
UpdateOne():
db.RecordDB.updateOne({"name":"Bhairavi"},{$set: {name: "Liv"}})
db.RecordDB.find()
UpdateMany():
db.RecordDB.updateMany({"name":"Bhairi"},{$set: {age: "25 Years"}})
db.RecordDB.find({"age":"25 Years"})
db.RecordDB.replaceOne({name:"Liv"}, {name: "Bhairavi"})
DeleteOne():
db.RecordDB.deleteOne({"name": "Bhairavi"})
DeleteMany():
db.RecordDB.deleteMany({"name": "Bhairi"})
db.RecordDB.find()
OUTPUT:
CONCLUSION:
This practical demonstrated how MongoDB is used to perform basic database operations on large
datasets. MongoDB's flexibility in handling unstructured data, combined with its scalability, makes
it ideal for Big Data applications. By using MongoDB's aggregation framework, students can
efficiently query and analyse data, providing valuable insights that are critical for Big Data
analytics. This practical reinforces the importance of NoSQL databases like MongoDB in
addressing the challenges posed by Big Data.