MongoDB - Interview Questions and Answers
MongoDB is a NoSQL database that stores data in a flexible, JSON-like format (BSON). It is schema-less and supports horizontal scaling.
BSON (Binary JSON) is MongoDB?s format for storing data, supporting additional types like Date and ObjectId.
Simply switch to a database name
use myDatabaseThe database is created when you insert data.
MongoDB creates collections dynamically when inserting documents:
db.createCollection("users")
db.users.insertOne({ name: "Alice" })
se insertOne() or insertMany()
db.users.insertOne({ name: "Alice", age: 25 })
db.users.insertMany([{ name: "Bob" }, { name: "Charlie" }])
db.users.find()
Use a query inside find()
db.users.find({ age: 25 })
Use projection:
db.users.find({}, { name: 1, _id: 0 })
db.users.find().sort({ age: -1 }) // Sort by age descending
db.users.find().limit(5)
db.users.updateOne({ name: "Alice" }, { $set: { age: 26 } })
db.users.updateMany({}, { $set: { status: "active" } })
db.users.updateOne({ name: "Alice" }, { $inc: { age: 1 } })
db.users.updateMany({}, { $rename: { "oldField": "newField" } })
db.users.replaceOne({ name: "Alice" }, { name: "Alice", age: 30, city: "NY" })
db.users.deleteOne({ name: "Alice" })
db.users.deleteMany({ age: { $lt: 18 } })
db.users.drop()
db.dropDatabase()
deleteOne() removes the first matching document, while remove() can remove multiple.
An index improves query performance by speeding up searches.
db.users.createIndex({ name: 1 }) // Ascending index
db.users.createIndex({ email: 1 }, { unique: true })
db.users.getIndexes()
db.users.dropIndex("name_1")
Aggregation processes data and returns computed results, like GROUP BY in SQL.
db.users.aggregate([
{ $match: { age: { $gt: 20 } } },
{ $group: { _id: "$city", total: { $sum: 1 } } }
])
Filters documents (like WHERE in SQL).
Groups data based on a field.
Selects which fields to include in the output.
Use $lookup:
db.orders.aggregate([
{ $lookup: { from: "users", localField: "userId", foreignField: "_id", as: "userDetails" } }
])
Embedding stores data inside the document, referencing stores IDs.
When data is frequently read together.
When data is large and frequently updated.
Use arrays or references.
Yes, since version 4.0.
const session = db.getMongo().startSession();
session.startTransaction();
Use transactions, write concerns, and validation rules.
db.createUser({ user: "admin", pwd: "password", roles: ["readWrite"] })
Use --auth when starting MongoDB
Replication keeps multiple copies of data for high availability.
A group of MongoDB servers with primary and secondary nodes.
Sharding distributes data across multiple machines.
When the dataset is too large for a single server.
rs.status()
A cloud database service for MongoDB.
A fixed-size collection that overwrites old data.
mongodump --db myDatabase
mongorestore --db myDatabase dump/
A MongoDB feature for storing large files.
Tutorials
Random Blogs
- String Operations in Python
- Government Datasets from 50 Countries for Machine Learning Training
- How AI is Making Humans Weaker – The Hidden Impact of Artificial Intelligence
- Internet of Things (IoT) & AI – Smart Devices and AI Working Together
- Datasets for Speech Recognition Analysis
- The Ultimate Guide to Starting a Career in Computer Vision
- Mastering SQL in 2025: A Complete Roadmap for Beginners
- Understanding OLTP vs OLAP Databases: How SQL Handles Query Optimization
- Extract RGB Color From a Image Using CV2
- Top 15 Recommended SEO Tools
