0% found this document useful (0 votes)
32 views

Mongo DB Demo 1715145434190

Uploaded by

wifpem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Mongo DB Demo 1715145434190

Uploaded by

wifpem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Mepco Schlenk Engineering College (Autonomous), Sivakasi

Department of Computer Science and Engineering


19CS452 – Database Systems Lab
MONGODB Tutorial

Create a student database in MongoDB using DDL and perform basic DML operations:
Creating a Database: MongoDB does not have a specific command to create a database
explicitly. Instead, you can switch to a non-existent database or use a database, and
MongoDB will create it automatically when you start adding data to it. For example:
• use studentdb

This command will switch to the studentdb database. If it doesn't exist, MongoDB will
create it.
• Creating a Collection:
Collections in MongoDB are analogous to tables in relational databases. You can create a
collection using the createCollection() method.
• db.createCollection("students")

This will create a collection named "students" in the studentdb database.


• Inserting Data:
You can insert documents (equivalent to rows in SQL) into the collection using the
insertOne() or insertMany() methods.
• db.students.insertOne({ name: "John", age: 20, grade: "A" })

This will insert a single document into the "students" collection with the specified fields.
// Define an array of student documents to insert
const studentsData = [
{ name: "Alice", age: 22, grade: "B" },
{ name: "Bob", age: 20, grade: "A" },
{ name: "Charlie", age: 21, grade: "C" }
];
// Insert multiple documents into the "students" collection
db.students.insertMany(studentsData)
In this example, we have an array studentsData containing multiple student documents. We
then use the insertMany() method to insert all these documents into the "students" collection
in the MongoDB database. Each document in the array represents a student with fields such
as name, age, and grade. This command will insert all the documents from the studentsData
array into the "students" collection. If the collection doesn't exist, MongoDB will create it
automatically.
• Querying Data:
To retrieve data from the collection, you can use the find() method.
• db.students.find()

This will return all documents from the "students" collection.


Querying with a Condition: You can use the find() method with a query condition to
retrieve documents that match specific criteria.
• // Find students with age greater than 18
db.students.find({ age: { $gt: 18 } })
This command will return all documents from the "students" collection where
the "age" field is greater than 18. The result will be an array of
documents, each representing a student. Here's an example result:
[
{ "_id": ObjectId("60c9d18f499f5b4b152f44d5"), "name": "Alice", "age":
22, "grade": "B" },
{ "_id": ObjectId("60c9d18f499f5b4b152f44d6"), "name": "Bob", "age":
20, "grade": "A" },
{ "_id": ObjectId("60c9d18f499f5b4b152f44d7"), "name": "Charlie",
"age": 21, "grade": "C" }
]

• Querying with Projection: You can specify which fields you want to retrieve using
projection.
• // Retrieve only the names of students
db.students.find({}, { name: 1, _id: 0 })
This command will return all documents from the "students" collection, but
only include the "name" field for each document. The "_id" field will be
excluded. Here's an example result:
[
{ "name": "Alice" },
{ "name": "Bob" },
{ "name": "Charlie" }
]

• Sorting Documents: You can sort the result set based on one or more fields.
• // Sort students by age in ascending order
db.students.find().sort({ age: 1 })

• Counting Documents: To count the number of documents that match a query, you can use
the count() method.
• // Count the number of students with age greater than 18
db.students.find({ age: { $gt: 18 } }).count()
This command will return the count of documents in the "students"
collection where the "age" field is greater than 18. If there are, for
example, 10 such documents, the result would be: 10. This indicates that
there are 10 students in the "students" collection with an age greater than
18.

• Limiting the Number of Results:


You can limit the number of documents returned by a query using the limit() method.
• // Limit the result to the first 5 students
db.students.find().limit(5)

• Skipping Results:
You can skip a certain number of documents from the beginning of the result set using the
skip() method.
• // Skip the first 5 students and retrieve the rest
db.students.find().skip(5)

• Aggregation:
MongoDB provides the aggregate() method for more complex operations like grouping,
filtering, and computing aggregates.
• // Calculate the average age of students

• db.students.aggregate([
{ $group: { _id: null, averageAge: { $avg: "$age" } } }
])

• Updating Data: You can update documents using the updateOne() or updateMany()
methods.
• db.students.updateOne(
{ name: "John" },
{ $set: { age: 21 } }
)
This will update the age of the student named "John" to 21.
• Deleting Data: To delete documents, you can use the deleteOne() or deleteMany()
methods.
• db.students.deleteOne({ name: "John" })
This will delete the document with the name "John" from the "students" collection.

Dr. S. Kavi Priya


ASP(SG)/CSE, MSEC

You might also like