Mongo DB Flashcards

1
Q

Why document database?

A

It stores data in a flexible, JSON-like format known as BSON (Binary JSON). Unlike relational databases that use tables and rows, MongoDB organizes data into documents within collections. Each document is a self-contained unit that can hold key-value pairs, arrays, and even nested objects, making it highly flexible and schema-less.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Saving to DB

A

client = MongoClient(“mongodb://localhost:27017/”)
db = client[“myDatabase”]
collection = db[“myCollection”]

document = {“name”: “John”, “age”: 30, “city”: “New York”}
collection.insert_one(document)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Fetch from DB

A

client = MongoClient(“mongodb://localhost:27017/”)
db = client[“myDatabase”]
collection = db[“myCollection”]

Read all documents
for doc in collection.find():
print(doc)

Read with a filter
doc = collection.find_one({“name”: “John”})
print(doc)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Organization

A

In MongoDB, data is organized in a hierarchical and flexible structure rather than rigid tables and rows like relational databases
📂 Database → A logical container for collections (like a schema in SQL).
📁 Collection → A group of related documents (like a table in SQL).
📜 Document → A JSON-like BSON (Binary JSON) object storing key-value pairs (like a row in SQL).
Field → A key-value pair inside a document (like a column in SQL).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Schemaless

A

Schema-less → Documents in the same collection can have different structures.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Nested Documents

A

Embedded/Nested Documents → Related data is stored together in a single document.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Indexing

A

Indexing for Fast Access → Indexes improve query performance.
Single-Field Index
Indexes a single field, improving search performance.
db.users.createIndex({ name: 1 }) # Ascending order
db.users.find({ name: “Alice” })

Compound Index

Indexes multiple fields for efficient searches involving both fields.
db.users.createIndex({ name: 1, age: -1 }) # First sort by name, then by age descending
db.users.find({ name: “Alice”, age: 25 })

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Types of Indexes

A

Single Field
Compound Field
Multikey Index - Indexing array based field
Text Index - Full text search on a field
GeoSpatial Index - Used for queries on location-based data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

List Indexes

A

db.users.getIndexes()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Drop Indexes

A

db.users.dropIndex(“name_1”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Use Cases

A

Hierarchical & Nested Data - User Orders, Social Media Posts
{
“_id”: 101,
“customer”: “John Doe”,
“items”: [
{ “product”: “Laptop”, “price”: 1200, “quantity”: 1 },
{ “product”: “Mouse”, “price”: 25, “quantity”: 2 }
],
“status”: “shipped”
}

Session Data
✔ Ideal for: High-speed session storage
✔ Example: Authentication tokens, shopping carts
{
“session_id”: “xyz123”,
“user_id”: 1001,
“last_active”: “2024-03-05T12:00:00Z”
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why

A

Embedded documents eliminate costly JOIN operations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

When not to use

A

Highly relational data (e.g., banking transactions, ERP systems) – Joins are required here.
Heavy ACID compliance needs (MongoDB provides transactions but not as strong as SQL)
Fixed-schema datasets (Better suited for RDBMS like MySQL, PostgreSQL)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Highly Relational Data - What is

A

Highly relational data consists of interconnected entities where strong data integrity, consistency, and ACID compliance are critical. This type of data is best suited for relational databases (RDBMS) like MySQL, PostgreSQL, or Oracle because they enforce foreign keys, normalization, and transactions.
Example banking data
A bank manages customers, accounts, transactions, and branches. Each entity has strong relationships, and data integrity is crucial.

MongoDB does not support traditional SQL-style joins, making complex queries inefficient.
Since MongoDB stores denormalized data (embedding), updates can be redundant and inconsistent.
Ex - If a customer’s email changes, you must update multiple documents.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Multi Document Transactions

A

MongoDB supports transactions (starting from v4.0), but they are not as efficient as SQL databases.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Loosely Related Data vs Highly Related Data

A

While MongoDB is not the best for highly relational data, it can work for loosely related data by: ✔ Embedding documents (one-to-many, when reads are frequent).
✔ Using references ($lookup) for many-to-many relationships (at a performance cost).
✔ Denormalization for read-heavy workloads (faster reads but more storage).

17
Q

Joins in Mongodb

A

MongoDB does not support traditional SQL joins, but it provides an alternative using the $lookup aggregation stage to join documents from multiple collections.
❌ Slower than SQL Joins → MongoDB is not optimized for cross-collection queries.
❌ Not Suitable for Deeply Nested Joins → Performance drops with multiple $lookup stages.
❌ No Index Support for $lookup Queries → Lookups scan the entire foreign collection.
❌ One-to-Many is Limited → Large referenced datasets may return huge arrays, affecting performance.