Cycle 04
Lab Cycle 4 — NoSQL Database Operations with MongoDB
MongoDB Document Updates, Deletions, Projections & Pagination
Problem Statement
Write MongoDB shell commands to: 1. Update the record of 'Geeta', setting city = 'Bombay' and phone = '11223344'. 2. Delete all records from parts where price is greater than 5000. 3. Display only the name (sname) and city of the supplier (suppressing _id). 4. Sort all suppliers by city and display only the first two records.
// 1. Update record of 'Geeta' setting city = 'Bombay' and phone = '11223344'
db.suppliers.updateOne(
{ sname: "Geeta" },
{ $set: { city: "Bombay", phone: "11223344" } }
);
// 2. Delete all records where price > 5000
db.parts.deleteMany(
{ price: { $gt: 5000 } }
);
// 3. Display only sname and city (exclude _id via projection)
db.suppliers.find(
{},
{ sname: 1, city: 1, _id: 0 }
);
// 4. Sort all suppliers by city ascending and display only first 2 records
db.suppliers.find()
.sort({ city: 1 })
.limit(2);
Question 2 of 2