MongoDB Assignment- 2

Basic Questions

  1. Write a MongoDB command to create a new database named schoolDB.
  2. How do you switch databases in the MongoDB shell?
  3. Write a command to rename a database in MongoDB.
  4. How can you delete/drop a database in MongoDB?
  5. Write a command to view statistics of the current database.
  6. Create a new collection named students inside schoolDB.
  7. Insert one student document with fields {name, age, grade} into students.
  8. Insert multiple student documents using insertMany.
  9. Write a query to find all documents in the students collection.
  10. Write a query to find one student whose name is “Rahul”.
  11. Update the grade of one student using updateOne.
  12. Update the age of multiple students who are in grade “10”.
  13. Replace an entire student document using replaceOne.
  14. Delete one student whose name is “Amit”.
  15. Delete all students with grade “Fail”.
  16. Write a query using $eq to find students with age = 18.
  17. Write a query using $ne to find students whose grade is not “A”.
  18. Write a query using $gt to find students older than 20 years.
  19. Write a query using $lt to find students younger than 15 years.
  20. Use $and to find students with grade “A” and age > 18.

Intermediate Questions

  1. Write a query using $or to find students in grade “A” or “B”.
  2. Write a query using $not to exclude students younger than 18.
  3. Use $nor to find students who are not in grade “C” and not older than 25.
  4. Write a query using $exists to find documents where the field “email” exists.
  5. Write a query using $type to find students whose age is stored as a number.
  6. Use $all to find students who know both “Math” and “Science” (array field: subjects).
  7. Use $size to find students who have exactly 3 subjects in their subjects array.
  8. Use $elemMatch to find students with at least one exam {score: >80, subject: “Math”}.
  9. Create a query with nested fields (example: find students where address.city = “Delhi”).
  10. Write a query to update multiple documents and increment age by 1.
  11. Write a query to update multiple documents and add a new field “status”: “active”.
  12. Demonstrate upsert in MongoDB with updateOne.
  13. Use projection to display only name and grade fields of students.
  14. Write a query to sort students by age in descending order.
  15. Write a query to limit the result to 5 students.
  16. Combine limit and skip to display students 6 to 10 from the collection.
  17. Write a query using $in to find students in grades “A”, “B”, “C”.
  18. Write a query using $nin to exclude students in grades “D”, “F”.
  19. Create a query to count the number of students in grade “A”.
  20. Use distinct to list all unique values of the grade field.

Advanced Questions

  1. Design a MongoDB query to delete all documents where status=”inactive” and lastLogin < 2020.
  2. Write a query combining $and, $or, and $not to find students with grade=”A” or age>20 but not living in “Delhi”.
  3. Write a query using $elemMatch to find students who scored >90 in English AND <60 in Math (array of exams).
  4. Demonstrate how to perform a bulk insert and bulk update operation in MongoDB.
  5. Write a query to group students by grade and count how many students are in each grade.
  6. Write a query to find students where address.zipcode is missing or null.
  7. Demonstrate a query to update all students by adding a lastUpdated field with the current timestamp.
  8. Write a query to delete documents that match multiple conditions using $and.
  9. Write a query that uses regex to find students whose names start with “A”.
  10. Write a MongoDB query using $and that combines $eq (grade=”A”), $in (age in [18,19,20]), and $exists (email field) to filter students.