Core MongoDB Concepts - Document oriented model Flashcards
What is a document-oriented database?
A NoSQL database that stores data in documents (JSON-like format) instead of tables
Name three popular document-oriented databases
- MongoDB
- CouchDB
- RavenDB
What is the primary data format used in MongoDB?
BSON (Binary JSON) → a binary representation of JSON-like documents
What is the structure of a MongoDB document?
Key-value pairs in JSON-like format:
{
“_id”: ObjectId(“123”),
“name”: “Alice”,
“age”: 25
}
How do you insert a single document in MongoDB?
db.users.insertOne({ name: “Alice”, age: 25 });
How do you insert multiple documents in MongoDB?
db.users.insertMany([
{ name: “Alice”, age: 25 },
{ name: “Bob”, age: 30 }
]);
What is the difference between SQL row and MongoDB document?
- SQL row → Tabular record with fixed columns.
- MongoDB document → Flexible schema with key-value pairs.
What is an embedded document in MongoDB?
A document nested inside another document:
{
“name”: “Alice”,
“address”: { “city”: “NY”, “zip”: “10001” }
}
What is the benefit of using embedded documents?
Reduces the need for JOIN operations → improves performance
What is a referenced document in MongoDB?
A document storing ObjectId references to another collection:
{ “userId”: ObjectId(“60f7…”), “orderId”: ObjectId(“61f8…”) }
How do you retrieve all documents from a collection in MongoDB?
db.users.find({});
How do you filter documents by field value in MongoDB?
db.users.find({ age: { $gt: 25 } });
What is the difference between ObjectId and UUID in MongoDB?
- ObjectId: 12-byte identifier used as a primary key.
- UUID: Universally unique identifier, often used for external references.
How do you sort documents in MongoDB?
db.users.find().sort({ age: -1 }); // Descending
How do you limit the number of documents returned?
db.users.find().limit(5);
How do you update a document in MongoDB?
db.users.updateOne(
{ name: “Alice” },
{ $set: { age: 26 } }
);
How do you delete a document in MongoDB?
db.users.deleteOne({ name: “Alice” });
What is indexing in document-oriented databases?
Improves query performance by creating efficient lookup keys
How do you create an index in MongoDB?
db.users.createIndex({ name: 1 });
How do you aggregate documents in MongoDB?
db.users.aggregate([
{ $match: { age: { $gt: 25 } } },
{ $group: { _id: “$city”, total: { $sum: 1 } } }
]);
What is the difference between aggregation and map-reduce?
- Aggregation: Simplifies data processing in pipelines.
- Map-reduce: Used for complex computations (slower in MongoDB).
How do you populate referenced documents in Mongoose?
User.find().populate(‘orders’).exec();
What is the benefit of document sharding in MongoDB?
Horizontal scaling → splits large collections across multiple servers
What is replication in MongoDB?
Copies of data are distributed across multiple servers for fault tolerance
How do you count documents in MongoDB?
db.users.countDocuments();
What is capped collection in MongoDB?
A fixed-size collection that automatically overwrites old documents
How do you create a capped collection?
db.createCollection(“logs”, { capped: true, size: 10000 });
What is the difference between normalize and denormalize in MongoDB?
- Normalize → Store references (linked collections).
- Denormalize → Store embedded documents (improves read performance).
How do you add a unique constraint in MongoDB?
db.users.createIndex({ email: 1 }, { unique: true });
What are common use cases for document-oriented databases?
- Content management systems (CMS)
- E-commerce product catalogs
- Real-time analytics
- IoT applications