db.mystudents.
insertMany([
{ sid: 1, sname: "Ajit", school: "Xavier", hobby: "Dance", std: 5, gender: "m" },
{ sid: 2, sname: "Alisha", school: "Sharda", hobby: "Singing", std: 5, gender:
"f" },
{ sid: 3, sname: "Pratixa", school: "Aone", hobby: "Cricket", std: 5, gender: "f"
},
{ sid: 4, sname: "Deepam", school: "HBK", hobby: "Painting", std: 6, gender:
"f" },
{ sid: 5, sname: "Nisha", school: "SantKabir", hobby: "Cricket", std: 7, gender:
"f" },
{ sid: 6, sname: "Anya", school: "Xavier", hobby: "Singing", std: 6, gender:
"f" },
{ sid: 7, sname: "Raj", school: "Aone", hobby: "Painting", std: 6, gender: "m" },
{ sid: 8, sname: "Monish", school: "Sharda", hobby: "Karate", std: 6, gender: "m"
},
{ sid: 9, sname: "Ram", school: "Aone", hobby: "Dance", std: 6, gender: "m" },
{ sid: 10, sname: "Shyam", school: "SantKabir", hobby: "Singing", std: 6, gender:
"m" },
{ sid: 11, sname: "Kavisha", school: "Xavier", hobby: "Swimming", std: 7, gender:
"f" },
{ sid: 12, sname: "Nayan", school: "Aone", hobby: "Cricket", std: 7, gender:
"m" },
{ sid: 13, sname: "Meet", school: "Somlalit", hobby: "Cricket", std: 7, gender:
"m" },
{ sid: 14, sname: "Urvi", school: "Sharda", hobby: "Dance", std: 7, gender:
"f" },
{ sid: 15, sname: "Kunj", school: "Xavier", hobby: "Singing", std: 8, gender: "m"
}
]);
List out the names of the mystudentss.
db.mystudents.find({}, { sname: 1, _id: 0 });
Retrieve the list of Name, School, and Std of all the mystudentss.
db.mystudents.find({}, { sname: 1, school: 1, std: 1, _id: 0 });
List all the mystudentss who are studying in Sharda.
db.mystudents.find({ school: "Sharda" });
Find the name of the mystudents whose name starts with ‘R’.
db.mystudents.find({ sname: { $regex: "^R" } }, { sname: 1, _id: 0 });
Find the name of the mystudents whose name starts with vowels.
db.mystudents.find({ sname: { $regex: "^[AEIOUaeiou]" } }, { sname: 1, _id: 0 });
Find the name of the mystudents whose name starts with consonants.
db.mystudents.find({ sname: { $regex: "^[^AEIOUaeiou]" } }, { sname: 1, _id: 0 });
Display all the records in ascending order of the name.
db.mystudents.find().sort({ sname: 1 });
Display all the records in ascending order of the school name and descending order
of the number.
db.mystudents.find().sort({ school: 1, std: -1 });
List all the schools.
db.mystudents.distinct("school");
Count the total number of mystudentss.
db.mystudents.countDocuments();
Add 4 new columns: maths, sci, eng, and fees.
db.mystudents.updateMany({}, { $set: { maths: 0, sci: 0, eng: 0, fees: 0 } });
Set fees for each standard.
db.mystudents.updateMany({ std: 5 }, { $set: { fees: 5000 } });
db.mystudents.updateMany({ std: 6 }, { $set: { fees: 6000 } });
db.mystudents.updateMany({ std: 7 }, { $set: { fees: 7000 } });
db.mystudents.updateMany({ std: 8 }, { $set: { fees: 8000 } });
Add marks for each mystudents.
db.mystudents.updateOne({ sid: 1 }, { $set: { maths: 80, sci: 70, eng: 75 } });
// Repeat for all mystudentss as needed.
Count the total male mystudentss.
db.mystudents.countDocuments({ gender: "m" });
List the names of the boys.
db.mystudents.find({ gender: "m" }, { sname: 1, _id: 0 });
List the names of the boys in Sharda.
db.mystudents.find({ gender: "m", school: "Sharda" }, { sname: 1, _id: 0 });
List the records whose hobby is cricket and from Somlalit.
db.mystudents.find({ hobby: "Cricket", school: "Somlalit" });
Count the total fees.
db.mystudents.aggregate([{ $group: { _id: null, totalFees: { $sum: "$fees" } } }]);
Display unique fees.
db.mystudents.distinct("fees");
Display max, min, and sum of fees.
db.mystudents.aggregate([
{ $group: { _id: null, maxFees: { $max: "$fees" }, minFees: { $min: "$fees" },
sumFees: { $sum: "$fees" } } }
]);
Display mystudentss by result in ascending order.
db.mystudents.aggregate([
{ $addFields: { totalMarks: { $add: ["$maths", "$sci", "$eng"] } } },
{ $sort: { totalMarks: 1 } }
]);
List records of mystudentss in std 7, male, and school name is Sharda.
db.mystudents.find({ std: 7, gender: "m", school: "Sharda" });
Delete all records of Xavier.
db.mystudents.deleteMany({ school: "Xavier" });
Change the school name "Sharda" to "DPS".
db.mystudents.updateMany({ school: "Sharda" }, { $set: { school: "DPS" } });
Change fees for Std 5 mystudentss from 5000 to 7000.
db.mystudents.updateMany({ std: 5, fees: 5000 }, { $set: { fees: 7000 } });
Change fees for the mystudents with the last rank to 10000.
db.mystudents.updateOne({ sid: 15 }, { $set: { fees: 10000 } });
Delete all mystudentss whose total marks are <50.
db.mystudents.deleteMany({ $expr: { $lt: [{ $add: ["$maths", "$sci", "$eng"] }, 50]
} });