Listen to this Post
1️⃣ What is MongoDB and why use NoSQL?
2️⃣ Difference between MongoDB and SQL databases?
3️⃣ What are Collections and Documents?
4️⃣ How do you perform Aggregation in MongoDB?
5️⃣ Explain Indexes in MongoDB and their use.
6️⃣ How to update multiple documents at once?
7️⃣ Difference between find() and aggregate()?
8️⃣ How to handle relationships (Embedding vs. Referencing)?
9️⃣ What is a Replica Set?
1️⃣0️⃣ How does Sharding work in MongoDB?
You Should Know:
Here are some practical MongoDB commands and codes to help you master these concepts:
1. Connect to MongoDB:
mongo --host <hostname> --port <port> -u <username> -p <password>
2. Create a Database:
use myDatabase;
3. Create a Collection and Insert a Document:
db.myCollection.insertOne({ name: "John", age: 30 });
4. Find Documents:
db.myCollection.find({ age: { $gt: 25 } });
5. Update Multiple Documents:
db.myCollection.updateMany({ age: { $lt: 30 } }, { $set: { status: "young" } });
6. Aggregation Example:
db.myCollection.aggregate([
{ $match: { age: { $gt: 25 } } },
{ $group: { _id: "$status", total: { $sum: 1 } } }
]);
7. Create an Index:
db.myCollection.createIndex({ name: 1 });
8. Check Replica Set Status:
rs.status();
9. Enable Sharding:
sh.enableSharding("myDatabase");
sh.shardCollection("myDatabase.myCollection", { _id: "hashed" });
10. Embedding vs. Referencing Example:
- Embedding:
db.users.insertOne({ name: "Alice", address: { city: "New York", zip: "10001" } }); - Referencing:
db.users.insertOne({ name: "Bob", addressId: ObjectId("12345") }); db.addresses.insertOne({ _id: ObjectId("12345"), city: "Los Angeles", zip: "90001" });
What Undercode Say:
MongoDB is a powerful NoSQL database that offers flexibility and scalability for modern applications. Mastering these interview questions and commands will give you a strong foundation for working with MongoDB in real-world scenarios. Practice these commands in a local or cloud MongoDB environment to solidify your understanding. For further learning, explore the official MongoDB documentation.
Additionally, here are some Linux commands to manage MongoDB:
– Start MongoDB Service:
sudo systemctl start mongod
– Stop MongoDB Service:
sudo systemctl stop mongod
– Check MongoDB Logs:
tail -f /var/log/mongodb/mongod.log
– Backup MongoDB Database:
mongodump --db myDatabase --out /backup/
– Restore MongoDB Database:
mongorestore --db myDatabase /backup/myDatabase/
Keep practicing and exploring MongoDB to excel in your tech career!
References:
Reported By: Sumit Yadav – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅



