MongoDB Assignment- 4

Basic Questions

  1. Create a MongoDB collection named students and insert a single document with fields: name, age, and grade.
  2. Demonstrate single-document atomicity by updating two fields (age and grade) of one student in a single operation.
  3. Insert multiple documents into orders collection using insertMany.
  4. Perform a single-document transaction that inserts a document into customers.
  5. Create a user with read permission on a database shopDB.
  6. Show how to list all existing users in the current MongoDB database.
  7. Write a query to find all documents in the books collection where price > 500.
  8. Demonstrate how to back up a database named college using mongodump.
  9. Restore the college database from the created dump using mongorestore.
  10. Enable authentication for MongoDB and create an admin user.
  11. Insert a document into products collection with fields: name, category, price, and quantity.
  12. Use $set to update the price of a single product by its _id.
  13. Use $inc to increase the quantity of an item by 5.
  14. Create a role readWriteOrders that has read and write permissions only on the orders collection.
  15. Check the MongoDB server logs and identify when the last restart happened.
  16. Show how to configure a replica set with a single primary and one secondary node.
  17. Run a query with read preference set to secondary.
  18. Update a document in employees and set writeConcern: { w: “majority” }.
  19. Check the status of the current replica set using rs.status().
  20. Use MongoDB Compass to analyze slow queries on a collection.

Intermediate Questions

  1. Write a transaction that transfers ₹100 from account A to account B in the bankAccounts collection.
  2. Demonstrate a multi-document transaction by inserting one document into orders and another into payments atomically.
  3. Show how to handle transaction rollback using abortTransaction.
  4. Run the same transaction once on a single‑node setup and once on a replica set, then display the difference in durability guarantees.
  5. Implement READ COMMITTED isolation level for a query.
  6. Open two MongoDB shells and attempt to update the same document concurrently, then observe which update “wins” via findOne().
  7. Create three users with different roles: read, readWrite, and dbAdmin, then test their permissions.
  8. Use RBAC to allow a user to access only the sales collection inside businessDB.
  9. Perform an encrypted password-based login for MongoDB using mongo –username.
  10. Use mongodump to back up a database, then upload the resulting dump files to a cloud bucket (e.g., AWS S3).
  11. Restore only a single collection from a database backup.
  12. Analyze a slow query using explain(“executionStats”) and suggest optimization.
  13. Enable profiling in MongoDB and retrieve queries that took longer than 100ms.
  14. Configure a 3‑node replica set, stop the primary, and run rs.status() to demonstrate automatic failover.
  15. Insert a document into orders with writeConcern:2, then stop one secondary and observe failure response.
  16. Create a query that reads from the nearest node in a replica set.
  17. Demonstrate how to use writeConcern: “majority” for ensuring durability.
  18. Use MongoDB Atlas to set up monitoring alerts for high CPU usage.
  19. Perform a performance test by indexing the email field of users and compare query times with/without index.
  20. Demonstrate sharding a large collection across multiple servers in Atlas.

Advanced Questions

  1. Implement a two-phase commit for a multi-document transaction in MongoDB.
  2. Start two transactions updating the same set of docs to simulate a deadlock, then investigate logs to see conflict resolution.
  3. Build a complete banking system simulation where multiple users deposit/withdraw simultaneously, ensuring consistency with transactions.
  4. Add an arbiter node to a replica set and run rs.status() to show its vote during elections.
  5. Begin a transaction, kill the primary mongod process before commit, then reconnect and verify rollback of the uncommitted transaction.
  6. Create a user-defined role that allows creating backups, restoring, and monitoring but not modifying data.
  7. Write a script to monitor slow queries in real-time and log them into a custom logs collection.
  8. Partition a 3‑node replica set (simulate network block), then attempt writes/reads and note the results with readPreference.
  9. Deploy a multi‑region cluster in MongoDB Atlas, then run queries from Asia vs Europe clients with region‑specific read preferences.
  10. Implement transaction retry logic in Node.js when a transient transaction error occurs.