Core MongoDB Concepts - Document oriented model Flashcards

1
Q

What is a document-oriented database?

A

A NoSQL database that stores data in documents (JSON-like format) instead of tables

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

Name three popular document-oriented databases

A
  • MongoDB
  • CouchDB
  • RavenDB
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the primary data format used in MongoDB?

A

BSON (Binary JSON) → a binary representation of JSON-like documents

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

What is the structure of a MongoDB document?

A

Key-value pairs in JSON-like format:
{
“_id”: ObjectId(“123”),
“name”: “Alice”,
“age”: 25
}

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

How do you insert a single document in MongoDB?

A

db.users.insertOne({ name: “Alice”, age: 25 });

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

How do you insert multiple documents in MongoDB?

A

db.users.insertMany([
{ name: “Alice”, age: 25 },
{ name: “Bob”, age: 30 }
]);

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

What is the difference between SQL row and MongoDB document?

A
  • SQL row → Tabular record with fixed columns.
  • MongoDB document → Flexible schema with key-value pairs.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is an embedded document in MongoDB?

A

A document nested inside another document:
{
“name”: “Alice”,
“address”: { “city”: “NY”, “zip”: “10001” }
}

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

What is the benefit of using embedded documents?

A

Reduces the need for JOIN operations → improves performance

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

What is a referenced document in MongoDB?

A

A document storing ObjectId references to another collection:
{ “userId”: ObjectId(“60f7…”), “orderId”: ObjectId(“61f8…”) }

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

How do you retrieve all documents from a collection in MongoDB?

A

db.users.find({});

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

How do you filter documents by field value in MongoDB?

A

db.users.find({ age: { $gt: 25 } });

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

What is the difference between ObjectId and UUID in MongoDB?

A
  • ObjectId: 12-byte identifier used as a primary key.
  • UUID: Universally unique identifier, often used for external references.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you sort documents in MongoDB?

A

db.users.find().sort({ age: -1 }); // Descending

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

How do you limit the number of documents returned?

A

db.users.find().limit(5);

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

How do you update a document in MongoDB?

A

db.users.updateOne(
{ name: “Alice” },
{ $set: { age: 26 } }
);

17
Q

How do you delete a document in MongoDB?

A

db.users.deleteOne({ name: “Alice” });

18
Q

What is indexing in document-oriented databases?

A

Improves query performance by creating efficient lookup keys

19
Q

How do you create an index in MongoDB?

A

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

20
Q

How do you aggregate documents in MongoDB?

A

db.users.aggregate([
{ $match: { age: { $gt: 25 } } },
{ $group: { _id: “$city”, total: { $sum: 1 } } }
]);

21
Q

What is the difference between aggregation and map-reduce?

A
  • Aggregation: Simplifies data processing in pipelines.
  • Map-reduce: Used for complex computations (slower in MongoDB).
22
Q

How do you populate referenced documents in Mongoose?

A

User.find().populate(‘orders’).exec();

23
Q

What is the benefit of document sharding in MongoDB?

A

Horizontal scaling → splits large collections across multiple servers

24
Q

What is replication in MongoDB?

A

Copies of data are distributed across multiple servers for fault tolerance

25
Q

How do you count documents in MongoDB?

A

db.users.countDocuments();

26
Q

What is capped collection in MongoDB?

A

A fixed-size collection that automatically overwrites old documents

27
Q

How do you create a capped collection?

A

db.createCollection(“logs”, { capped: true, size: 10000 });

28
Q

What is the difference between normalize and denormalize in MongoDB?

A
  • Normalize → Store references (linked collections).
  • Denormalize → Store embedded documents (improves read performance).
29
Q

How do you add a unique constraint in MongoDB?

A

db.users.createIndex({ email: 1 }, { unique: true });

30
Q

What are common use cases for document-oriented databases?

A
  • Content management systems (CMS)
  • E-commerce product catalogs
  • Real-time analytics
  • IoT applications