Mongo DB Demo 1715145434190
Mongo DB Demo 1715145434190
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 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()
• 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.
• 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.