MongoDB Assignment- 4
Basic Questions
- Create a MongoDB collection named students and insert a single document with fields: name, age, and grade.
- Demonstrate single-document atomicity by updating two fields (age and grade) of one student in a single operation.
- Insert multiple documents into orders collection using insertMany.
- Perform a single-document transaction that inserts a document into customers.
- Create a user with read permission on a database shopDB.
- Show how to list all existing users in the current MongoDB database.
- Write a query to find all documents in the books collection where price > 500.
- Demonstrate how to back up a database named college using mongodump.
- Restore the college database from the created dump using mongorestore.
- Enable authentication for MongoDB and create an admin user.
- Insert a document into products collection with fields: name, category, price, and quantity.
- Use $set to update the price of a single product by its _id.
- Use $inc to increase the quantity of an item by 5.
- Create a role readWriteOrders that has read and write permissions only on the orders collection.
- Check the MongoDB server logs and identify when the last restart happened.
- Show how to configure a replica set with a single primary and one secondary node.
- Run a query with read preference set to secondary.
- Update a document in employees and set writeConcern: { w: “majority” }.
- Check the status of the current replica set using rs.status().
- Use MongoDB Compass to analyze slow queries on a collection.
Intermediate Questions
- Write a transaction that transfers ₹100 from account A to account B in the bankAccounts collection.
- Demonstrate a multi-document transaction by inserting one document into orders and another into payments atomically.
- Show how to handle transaction rollback using abortTransaction.
- Run the same transaction once on a single‑node setup and once on a replica set, then display the difference in durability guarantees.
- Implement READ COMMITTED isolation level for a query.
- Open two MongoDB shells and attempt to update the same document concurrently, then observe which update “wins” via findOne().
- Create three users with different roles: read, readWrite, and dbAdmin, then test their permissions.
- Use RBAC to allow a user to access only the sales collection inside businessDB.
- Perform an encrypted password-based login for MongoDB using mongo –username.
- Use mongodump to back up a database, then upload the resulting dump files to a cloud bucket (e.g., AWS S3).
- Restore only a single collection from a database backup.
- Analyze a slow query using explain(“executionStats”) and suggest optimization.
- Enable profiling in MongoDB and retrieve queries that took longer than 100ms.
- Configure a 3‑node replica set, stop the primary, and run rs.status() to demonstrate automatic failover.
- Insert a document into orders with writeConcern:2, then stop one secondary and observe failure response.
- Create a query that reads from the nearest node in a replica set.
- Demonstrate how to use writeConcern: “majority” for ensuring durability.
- Use MongoDB Atlas to set up monitoring alerts for high CPU usage.
- Perform a performance test by indexing the email field of users and compare query times with/without index.
- Demonstrate sharding a large collection across multiple servers in Atlas.
Advanced Questions
- Implement a two-phase commit for a multi-document transaction in MongoDB.
- Start two transactions updating the same set of docs to simulate a deadlock, then investigate logs to see conflict resolution.
- Build a complete banking system simulation where multiple users deposit/withdraw simultaneously, ensuring consistency with transactions.
- Add an arbiter node to a replica set and run rs.status() to show its vote during elections.
- Begin a transaction, kill the primary mongod process before commit, then reconnect and verify rollback of the uncommitted transaction.
- Create a user-defined role that allows creating backups, restoring, and monitoring but not modifying data.
- Write a script to monitor slow queries in real-time and log them into a custom logs collection.
- Partition a 3‑node replica set (simulate network block), then attempt writes/reads and note the results with readPreference.
- Deploy a multi‑region cluster in MongoDB Atlas, then run queries from Asia vs Europe clients with region‑specific read preferences.
- Implement transaction retry logic in Node.js when a transient transaction error occurs.