MongoDB
25 Questions
Practice Problems
MongoDB Interview Questions
25 Questions
MongoDB Basics
What is MongoDB?
MongoDB is a NoSQL database that stores data in flexible JSON-like documents instead of tables. It is designed for scalability, high performance, and handling large amounts of unstructured or semi-structured data in modern applications. Example: db.users.insertOne({name: "John"});
What are the features of MongoDB?
MongoDB provides flexible schemas, high scalability, indexing, replication, sharding, aggregation, and fast query performance. It stores data in BSON documents and supports handling large datasets efficiently, making it suitable for modern web and cloud applications. Example: db.products.find();
What is the difference between MongoDB and SQL databases?
MongoDB is a NoSQL database storing data as documents, while SQL databases store data in tables with rows and columns. MongoDB supports flexible schemas, whereas SQL databases use fixed schemas and structured relationships. Example: { name: "Sam", age: 22 }
What is a document in MongoDB?
A document is the basic data unit in MongoDB, similar to a row in SQL databases. Documents store data in key-value pairs using BSON format and can contain nested objects and arrays. Example: { name: "John", age: 25 }
What is a collection in MongoDB?
A collection is a group of MongoDB documents, similar to a table in relational databases. Collections do not require fixed schemas, allowing documents with different structures to exist within the same collection. Example: db.users.find();
What is BSON in MongoDB?
BSON stands for Binary JSON. It is the data format MongoDB uses internally to store documents efficiently. BSON supports additional data types like dates and binary data that regular JSON does not support directly. Example: { createdAt: new Date() }
What is the difference between collection and table?
A collection in MongoDB stores flexible documents without fixed schemas, while a table in SQL databases stores structured rows with predefined columns. Collections support dynamic data structures more easily than traditional tables. Example: db.students.insertOne({name: "Alex"});
What are the advantages of MongoDB?
MongoDB provides flexibility, scalability, fast performance, high availability, and easy handling of large datasets. Its document-based structure simplifies development for applications with changing or complex data requirements. Example: db.orders.find();
What is the default port of MongoDB?
MongoDB uses port 27017 as its default communication port. Applications connect to MongoDB servers using this port unless developers configure a different custom port manually. Example: mongodb://localhost:27017
What is the difference between MongoDB and MySQL?
MongoDB is a NoSQL document database with flexible schemas, while MySQL is a relational database using tables and fixed schemas. MongoDB handles unstructured data efficiently, whereas MySQL is better for structured relational data. Example: db.users.find();
CRUD Operations
What are CRUD operations in MongoDB?
CRUD stands for Create, Read, Update, and Delete. These are the basic database operations used to manage MongoDB documents and collections in applications. Example: db.users.insertOne({name: "Sam"});
How do you insert data in MongoDB?
Data is inserted using methods like insertOne() and insertMany(). These methods add one or multiple documents into a collection inside the MongoDB database. Example: db.users.insertOne({name: "John"});
How do you retrieve data in MongoDB?
Data retrieval is done using methods like find() and findOne(). These methods fetch documents from collections based on filtering conditions or query criteria. Example: db.users.find({age: 25});
How do you update data in MongoDB?
MongoDB updates documents using methods like updateOne(), updateMany(), or replaceOne(). Developers commonly use update operators like $set to modify existing document fields. Example: db.users.updateOne( {name: "John"}, {$set: {age: 30}} );
How do you delete data in MongoDB?
Documents are deleted using deleteOne() or deleteMany() methods. These methods remove matching documents from a collection based on specified query conditions. Example: db.users.deleteOne({name: "Sam"});
What is the difference between find() and findOne()?
find() returns multiple matching documents as a cursor, while findOne() returns only the first matching document directly. Developers use findOne() when only a single document is required. Example: db.users.findOne({name: "Alex"});
What is projection in MongoDB?
Projection controls which fields should appear in query results. It helps retrieve only necessary data instead of entire documents, improving query performance and reducing unnecessary data transfer. Example: db.users.find({}, {name: 1});
What are MongoDB operators?
MongoDB operators perform special operations in queries and updates. Common operators include $gt, $lt, $set, $in, and $or, helping filter, modify, and manage database documents efficiently. Example: db.users.find({age: {$gt: 18}});
What is the difference between updateOne() and updateMany()?
updateOne() updates only the first matching document, while updateMany() updates all matching documents in a collection. Developers choose based on whether single or multiple records need modification. Example: db.users.updateMany({}, {$set: {active: true}});
What is aggregation in MongoDB?
Aggregation processes and transforms data through multiple pipeline stages like filtering, grouping, sorting, and calculations. It is widely used for analytics, reporting, and generating summarized results from collections. Example: db.orders.aggregate([ {$group: {_id: "$status"}} ]);
Advanced MongoDB
What is indexing in MongoDB?
Indexing improves query performance by allowing MongoDB to locate documents faster without scanning the entire collection. Indexes are commonly created on frequently searched fields like usernames, emails, or product IDs. Example: db.users.createIndex({email: 1});
What is the purpose of _id in MongoDB?
_id is a unique identifier automatically created for every MongoDB document. It ensures each document can be uniquely identified and retrieved efficiently inside collections. Example: { _id: ObjectId() }
What is replication in MongoDB?
Replication copies data from one MongoDB server to multiple servers for backup and high availability. It ensures data remains accessible even if the primary server fails. Example: rs.initiate();
What is sharding in MongoDB?
Sharding distributes data across multiple servers to improve scalability and performance. It helps MongoDB handle massive datasets and high traffic by splitting collections into smaller manageable parts. Example: sh.enableSharding("mydb");
What is Mongoose in MongoDB?
Mongoose is an Object Data Modeling library for Node.js and MongoDB. It provides schemas, validation, middleware, and easier database interaction, simplifying MongoDB operations in backend applications. Example: const User = mongoose.model("User", schema);