Document Databases (MongoDB, Couchbase) Flashcards

MongoDB overview and document model BSON format Collections vs Tables Schema design in MongoDB CRUD operations using find, insertOne, updateOne, deleteOne Querying nested fields and arrays Indexes in MongoDB Aggregation pipeline Transactions in MongoDB Replication and sharding in MongoDB (35 cards)

1
Q

What is MongoDB?

A

MongoDB is a NoSQL, document-oriented database that stores data in flexible, JSON-like documents.

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

What is the document model in MongoDB?

A

The document model stores data as BSON documents which can include nested fields and arrays, allowing for more natural data modeling.

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

What is BSON in MongoDB?

A

BSON (Binary JSON) is a binary format used to store documents in MongoDB, supporting additional data types like Date and BinData.

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

How do MongoDB collections compare to relational database tables?

A

Collections are analogous to tables, but they do not enforce a schema, allowing documents with different structures in the same collection.

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

What is schema design in MongoDB?

A

Schema design in MongoDB involves structuring documents and collections to optimize for queries, updates, and scaling.

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

What is the purpose of insertOne in MongoDB?

A

insertOne is a CRUD operation that adds a single document to a collection.

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

What does find do in MongoDB?

A

find retrieves documents from a collection that match specified query criteria.

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

What does updateOne do in MongoDB?

A

updateOne modifies the first document that matches the filter criteria in a collection.

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

What does deleteOne do in MongoDB?

A

deleteOne removes the first document that matches the filter criteria from a collection.

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

How do you query nested fields in MongoDB?

A

Use dot notation, like find({ 'address.city': 'New York' }), to query nested fields.

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

How do you query arrays in MongoDB?

A

You can use operators like $elemMatch or $in to query array fields.

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

What are indexes in MongoDB?

A

Indexes improve query performance by allowing efficient data retrieval without scanning all documents.

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

What types of indexes does MongoDB support?

A

MongoDB supports single-field, compound, multikey, geospatial, text, and hashed indexes.

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

What is the aggregation pipeline in MongoDB?

A

A framework that processes documents through multiple stages like $match, $group, $project, etc., to transform and analyze data.

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

What is the purpose of transactions in MongoDB?

A

Transactions ensure that multiple operations across multiple documents execute as a single atomic unit, preserving data integrity.

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

What is replication in MongoDB?

A

Replication copies data from a primary node to secondary nodes, ensuring high availability and data redundancy.

17
Q

What is sharding in MongoDB?

A

Sharding distributes data across multiple machines (shards) to support horizontal scaling of large datasets.

18
Q

What are document databases?

A

Document databases store data in document format (JSON, BSON, XML), and each document is a self-contained unit of data.

19
Q

What are examples of document databases?

A

Examples include MongoDB, Couchbase, and Amazon DocumentDB.

20
Q

What are the advantages of document databases?

A

Advantages include flexible schema, faster development, natural data modeling, and easier horizontal scaling.

21
Q

What are the disadvantages of document databases?

A

Disadvantages include data duplication, limited support for complex joins, and higher memory usage.

22
Q

What are best practices for MongoDB schema design?

A

Model around application query patterns, embed related data when access is frequent, avoid deeply nested documents, and use proper indexing.

23
Q

When should you embed documents in MongoDB?

A

Embed when the relationship is one-to-few and data is accessed together, like a user and their addresses.

24
Q

When should you reference documents in MongoDB?

A

Reference when the relationship is one-to-many or many-to-many and data is accessed independently.

25
What are common use cases for MongoDB?
Content management, IoT, real-time analytics, mobile backends, catalogs, and personalization engines.
26
How does MongoDB affect system design?
It encourages flexible schema design, horizontal scalability, denormalization, and microservices-friendly architecture.
27
Give an example MongoDB document.
{ \"name\": \"John\", \"email\": \"john@example.com\", \"orders\": [ { \"item\": \"Book\", \"price\": 10 } ] }
28
What are architectural implications of using MongoDB?
MongoDB enables scalable, distributed systems with eventual consistency, and often requires designing around trade-offs in data modeling.
29
What is MongoDB's performance under high load?
It performs well with proper indexing and schema design, and supports scaling with sharding and replication.
30
How does MongoDB ensure fault tolerance?
Through replica sets, automatic failover, and self-healing nodes in case of crashes or maintenance.
31
How can you monitor MongoDB?
Using tools like MongoDB Atlas, Ops Manager, `mongostat`, and `mongotop` to monitor performance and resource usage.
32
How do you debug slow queries in MongoDB?
Use `explain()` to analyze query plans and the profiler to identify and log slow operations.
33
What are real-world tradeoffs with MongoDB?
Tradeoffs include faster dev and flexibility vs. increased complexity in enforcing consistency and handling joins.
34
What are common MongoDB interview questions?
Examples: 'Explain the aggregation pipeline', 'When to embed vs. reference?', 'How does sharding work?', 'Explain replica sets'.
35
What are potential gotchas with MongoDB?
Unbounded document growth, missing indexes causing slow queries, misuse of sharding keys, and lack of enforced schema leading to inconsistent data.