MongoDB Assignment- 2
Basic Questions
- Write a MongoDB command to create a new database named schoolDB.
- How do you switch databases in the MongoDB shell?
- Write a command to rename a database in MongoDB.
- How can you delete/drop a database in MongoDB?
- Write a command to view statistics of the current database.
- Create a new collection named students inside schoolDB.
- Insert one student document with fields {name, age, grade} into students.
- Insert multiple student documents using insertMany.
- Write a query to find all documents in the students collection.
- Write a query to find one student whose name is “Rahul”.
- Update the grade of one student using updateOne.
- Update the age of multiple students who are in grade “10”.
- Replace an entire student document using replaceOne.
- Delete one student whose name is “Amit”.
- Delete all students with grade “Fail”.
- Write a query using $eq to find students with age = 18.
- Write a query using $ne to find students whose grade is not “A”.
- Write a query using $gt to find students older than 20 years.
- Write a query using $lt to find students younger than 15 years.
- Use $and to find students with grade “A” and age > 18.
Intermediate Questions
- Write a query using $or to find students in grade “A” or “B”.
- Write a query using $not to exclude students younger than 18.
- Use $nor to find students who are not in grade “C” and not older than 25.
- Write a query using $exists to find documents where the field “email” exists.
- Write a query using $type to find students whose age is stored as a number.
- Use $all to find students who know both “Math” and “Science” (array field: subjects).
- Use $size to find students who have exactly 3 subjects in their subjects array.
- Use $elemMatch to find students with at least one exam {score: >80, subject: “Math”}.
- Create a query with nested fields (example: find students where address.city = “Delhi”).
- Write a query to update multiple documents and increment age by 1.
- Write a query to update multiple documents and add a new field “status”: “active”.
- Demonstrate upsert in MongoDB with updateOne.
- Use projection to display only name and grade fields of students.
- Write a query to sort students by age in descending order.
- Write a query to limit the result to 5 students.
- Combine limit and skip to display students 6 to 10 from the collection.
- Write a query using $in to find students in grades “A”, “B”, “C”.
- Write a query using $nin to exclude students in grades “D”, “F”.
- Create a query to count the number of students in grade “A”.
- Use distinct to list all unique values of the grade field.
Advanced Questions
- Design a MongoDB query to delete all documents where status=”inactive” and lastLogin < 2020.
- Write a query combining $and, $or, and $not to find students with grade=”A” or age>20 but not living in “Delhi”.
- Write a query using $elemMatch to find students who scored >90 in English AND <60 in Math (array of exams).
- Demonstrate how to perform a bulk insert and bulk update operation in MongoDB.
- Write a query to group students by grade and count how many students are in each grade.
- Write a query to find students where address.zipcode is missing or null.
- Demonstrate a query to update all students by adding a lastUpdated field with the current timestamp.
- Write a query to delete documents that match multiple conditions using $and.
- Write a query that uses regex to find students whose names start with “A”.
- Write a MongoDB query using $and that combines $eq (grade=”A”), $in (age in [18,19,20]), and $exists (email field) to filter students.