1Q. What is MongoDB?

Definition:
MongoDB is a NoSQL, document-oriented database used to store data in a flexible, JSON-like format.

Key Points:

  • Developed by MongoDB Inc.
  • Stores data as documents instead of tables
  • Uses BSON (Binary JSON) format
  • Designed for scalability, high performance, and flexibility

2Q. Why is MongoDB called a NoSQL database?

Definition:
MongoDB is called a NoSQL database because it does not use traditional SQL-based relational tables.

Key Reasons:

  • No fixed schema (schema-less design)
  • Does not rely on rows and columns
  • Uses collections and documents
  • Supports unstructured and semi-structured data

Meaning of NoSQL:

  • “Not Only SQL” → supports modern data storage approaches beyond relational models

3Q. How does MongoDB differ from relational databases?

FeatureMongoDBRelational Databases (RDBMS)
Data FormatDocument (JSON/BSON)Tables (Rows & Columns)
SchemaDynamic (Flexible)Fixed (Predefined Schema)
RelationshipsEmbedded or referencedForeign Keys
ScalabilityHorizontal (easy scaling)Vertical (limited scaling)
Query LanguageMongoDB Query Language (MQL)SQL
ExampleMongoDBMySQL, PostgreSQL

4Q. What is a document in MongoDB?

Definition:
A document is the basic unit of data in MongoDB.

Key Points:

  • Stored in JSON-like format (BSON)
  • Contains key-value pairs
  • Can have nested data (arrays, objects)

Example:

{
"name": "Jitendra",
"age": 22,
"skills": ["Node.js", "MongoDB"]
}

5Q. What is a collection in MongoDB?

Definition:
A collection is a group of related documents.

Key Points:

  • Equivalent to a table in relational databases
  • Does not enforce schema
  • Documents in a collection can have different structures

Example:

  • users collection → stores user documents
  • orders collection → stores order documents

6Q. What is a database in MongoDB?

Definition:
A database is a container for collections.

Key Points:

  • Holds multiple collections
  • Each database is independent
  • Used to organize data logically

Example Structure:

Database: ExamAdda
├── users (collection)
├── courses (collection)
└── payments (collection)

7Q. What is BSON?

Definition:
BSON (Binary JSON) is the internal data format used by MongoDB to store documents.

Key Points:

  • Extension of JSON in binary format
  • Optimized for storage and speed
  • Supports more data types than JSON

Why BSON?

  • Faster encoding/decoding
  • Efficient for querying and indexing
  • Supports types like Date, ObjectId, Binary

8Q. What are the advantages of MongoDB?

1. Schema Flexibility

  • No fixed structure → easy to modify data model

2. High Performance

  • Fast read/write operations
  • Efficient indexing

3. Horizontal Scalability

  • Supports sharding (data distributed across servers)

4. JSON-like Data Storage

  • Easy integration with modern apps (Node.js, APIs)

5. High Availability

  • Replica sets provide automatic failover

6. Developer Friendly

  • No complex joins → faster development

9Q. What are the limitations of MongoDB?

1. No Complex Joins (Limited)

  • Not as powerful as SQL joins (though $lookup exists)

2. Data Duplication

  • Denormalization can increase redundancy

3. Memory Usage

  • Requires more RAM for performance

4. Transactions (Historically Limited)

  • Multi-document transactions are slower than RDBMS

5. Not Ideal for Highly Structured Data

  • Financial systems often prefer relational databases

10Q. What data types are supported in MongoDB?

MongoDB supports a rich set of BSON data types:

Common Data Types:

  • String"name": "Jitendra"
  • Integer"age": 22
  • Double"price": 99.99
  • Boolean"isActive": true
  • Array"skills": ["Node.js", "MongoDB"]
  • Object (Embedded Document) → nested JSON

Special BSON Types:

  • ObjectId → unique identifier
  • Date → date/time values
  • Null
  • Binary Data
  • Timestamp
  • Decimal128

11Q. What is the default port of MongoDB?

Answer:
MongoDB runs on port 27017 by default.

Example:

mongodb://localhost:27017

12Q. What is the role of the _id field?

Definition:
The _id field is a unique identifier for each document in a collection.

Key Points:

  • Automatically created if not provided
  • Must be unique
  • Acts as the primary key

Features:

  • Default type is ObjectId
  • Used for indexing and fast lookup
  • Cannot be duplicated within a collection

Example:

{
"_id": "507f1f77bcf86cd799439011",
"name": "Jitendra"
}

13Q. How does MongoDB store data internally?

Definition:
MongoDB stores data in a binary format (BSON) inside collections.

Internal Storage Flow:

  • Data → Stored as documents (BSON)
  • Documents → Stored inside collections
  • Collections → Stored inside databases

Key Concepts:

  • Uses WiredTiger storage engine (default)
  • Data stored in compressed format
  • Uses indexes for fast queries
  • Supports memory-mapped files & caching

Important Features:

  • Document-level locking (better concurrency)
  • Journaling for data durability
  • Data stored on disk + cached in RAM

14Q. How do you create a database in MongoDB?

Method:
MongoDB creates a database automatically when you first store data in it.

Command:

use examadda

Explanation:

  • Switches to (or creates) database examadda
  • Database is created only after inserting data

15Q. How do you create a collection?

Method 1: Automatically

  • Created when you insert data

Method 2: Manually

db.createCollection("users")

Key Points:

  • Collections are schema-less
  • Can store different document structures

16Q. How do you insert a document into a collection?

Using insertOne():

db.users.insertOne({
name: "Jitendra",
age: 22
})

Using insertMany():

db.users.insertMany([
{ name: "A", age: 20 },
{ name: "B", age: 25 }
])

17Q. What is insertOne() vs insertMany()?

FeatureinsertOne()insertMany()
PurposeInsert single documentInsert multiple documents
InputOne objectArray of objects
PerformanceSlower for bulkFaster for bulk inserts
Use CaseSingle recordBulk data insertion

18Q. How do you read data using find()?

Basic Syntax:

db.users.find()

With Condition:

db.users.find({ age: 22 })

With Projection:

db.users.find({ age: 22 }, { name: 1, _id: 0 })

Key Points:

  • Returns a cursor (multiple documents)
  • Can filter, sort, limit results

19Q. What is the difference between find() and findOne()?

Featurefind()findOne()
Return TypeCursor (multiple documents)Single document
OutputArray-like resultOne object
Use CaseFetch multiple recordsFetch one record

Example:

db.users.findOne({ age: 22 })

20Q. How do you update a document?

Using updateOne():

db.users.updateOne(
{ name: "Jitendra" },
{ $set: { age: 23 } }
)

Using updateMany():

db.users.updateMany(
{ age: 22 },
{ $set: { status: "active" } }
)

Using replaceOne():

db.users.replaceOne(
{ name: "Jitendra" },
{ name: "Jitendra", age: 25 }
)

Important Operators:

  • $set → update field
  • $inc → increment value
  • $unset → remove field

22Q. What are CRUD operations?

Definition:
CRUD stands for the four basic database operations:

Operations:

  • C – Create → Insert data
    insertOne(), insertMany()
  • R – Read → Retrieve data
    find(), findOne()
  • U – Update → Modify data
    updateOne(), updateMany()
  • D – Delete → Remove data
    deleteOne(), deleteMany()

Key Point:
CRUD forms the foundation of any database interaction.


23Q. What is Mongo Shell?

Definition:
Mongo Shell is a command-line interface (CLI) used to interact with MongoDB.

Key Points:

  • Run queries and commands directly
  • Default shell: mongosh
  • Used for:
    • Database operations
    • Debugging
    • Administration

Example:

mongosh
use examadda
db.users.find()

24Q. What is MongoDB Compass?

Definition:
MongoDB Compass is a GUI (Graphical User Interface) tool for MongoDB.

Key Features:

  • Visualize data
  • Run queries without coding
  • Create indexes
  • Analyze performance

Developed by: MongoDB Inc.

Use Case:
Best for beginners and debugging complex queries visually


25Q. What is indexing in MongoDB?

Definition:
Indexing is a technique to improve query performance by creating a data structure that allows fast data retrieval.

Key Points:

  • Works like a book index
  • Stores a sorted order of field values
  • Avoids full collection scan

Example:

db.users.createIndex({ name: 1 })

26Q. Why are indexes important?

Benefits:

  • Faster Queries → Reduces search time
  • Efficient Sorting → Improves sort performance
  • Better Performance → Especially for large datasets

Trade-offs:

  • Takes extra storage space
  • Slows down write operations (insert/update)

27Q. What is a cursor in MongoDB?

Definition:
A cursor is a pointer to the result set of a query.

Key Points:

  • Returned by find()
  • Allows iteration over results
  • Fetches data in batches

Example:

const cursor = db.users.find()
while (cursor.hasNext()) {
printjson(cursor.next())
}

28Q. What is a namespace in MongoDB?

Definition:
A namespace is the combination of database and collection name.

Format:

database.collection

Example:

examadda.users

Key Point:
Each collection has a unique namespace within a database.


29Q. What are some common use cases of MongoDB?

1. Real-Time Applications

  • Chat apps, live feeds

2. Content Management Systems (CMS)

  • Blogs, media platforms

3. E-commerce Applications

  • Product catalogs, orders

4. Big Data & Analytics

  • Handling large, unstructured data

5. IoT Applications

  • Sensor data storage

6. Mobile & Web Apps

  • Flexible schema fits changing requirements

30Q. When should MongoDB be preferred over SQL databases?

Use MongoDB When:

1. Flexible Schema Needed

  • Data structure changes frequently

2. High Scalability Required

  • Need horizontal scaling (sharding)

3. Large Unstructured Data

  • JSON-like data, logs, analytics

4. Fast Development

  • No strict schema → rapid iteration

5. Real-Time Performance

  • High read/write throughput

Avoid MongoDB When:

  • Complex joins required
  • Strict ACID compliance needed (e.g., banking systems)