0% found this document useful (0 votes)
21 views2 pages

Mongo TP 1

The document provides a series of MongoDB commands for managing a student database, including inserting data, querying for students based on age and courses, updating student information, and deleting records. It also includes commands for aggregating data, such as calculating average grades and counting students per course. Additionally, it demonstrates how to create an index on the email field for improved search performance.

Uploaded by

tomas
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)
21 views2 pages

Mongo TP 1

The document provides a series of MongoDB commands for managing a student database, including inserting data, querying for students based on age and courses, updating student information, and deleting records. It also includes commands for aggregating data, such as calculating average grades and counting students per course. Additionally, it demonstrates how to create an index on the email field for improved search performance.

Uploaded by

tomas
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/ 2

1.

Insert the dataset

db.students.insertMany(<paste the content of students_large.json here>);

2. Find all students

db.students.find();

3. Find students older than 21

db.students.find({ age: { $gt: 21 } });

4. Show only name and email

db.students.find({}, { name: 1, email: 1, _id: 0 });

5. Find students enrolled in “Math”

db.students.find({ "courses.name": "Math" });

6. Find students with any grade over 90

db.students.find({ "courses.grade": { $gt: 90 } });

7. Sort students by age (descending)

db.students.find().sort({ age: -1 });

8. Update a student's email (example: Alice Smith)

db.students.updateOne(
{ name: "Alice Smith" },
{ $set: { email: "alice.smith@newmail.com" } }
);

9. Add a new course to Bob Johnson

db.students.updateOne(
{ name: "Bob Johnson" },
{ $push: { courses: { name: "Economics", grade: 87 } } }
);
10. Delete a student (example: Ivan Davis)

db.students.deleteOne({ name: "Ivan Davis" });

11. Count how many students are enrolled

db.students.countDocuments({ enrolled: true });

12. Find students in Math or Biology

db.students.find({
$or: [
{ "courses.name": "Math" },
{ "courses.name": "Biology" }
]
});

13. Average Math grade (aggregation)

db.students.aggregate([
{ $unwind: "$courses" },
{ $match: { "courses.name": "Math" } },
{ $group: { _id: null, averageMathGrade: { $avg: "$courses.grade" } } }
]);

14. Count students per course

db.students.aggregate([
{ $unwind: "$courses" },
{ $group: { _id: "$courses.name", studentCount: { $sum: 1 } } },
{ $sort: { studentCount: -1 } }
]);

15. Add averageGrade to each student

db.students.aggregate([
{
$addFields: {
averageGrade: { $avg: "$courses.grade" }
}
}
]);

16. Create index on email

db.students.createIndex({ email: 1 });

You might also like