⬅ Previous Topic
MongoDB Course IndexNext Topic ⮕
MongoDB Use-cases and Features Overview⬅ Previous Topic
MongoDB Course IndexNext Topic ⮕
MongoDB Use-cases and Features OverviewMongoDB is a document-oriented NoSQL database used for high-volume data storage. Instead of using tables and rows like traditional relational databases (SQL), MongoDB uses collections and documents.
Each document in MongoDB is a JSON-like object called BSON (Binary JSON), which allows for embedded data and flexible schemas.
MongoDB is widely used in modern applications due to its:
SQL (Relational) | NoSQL (MongoDB) |
---|---|
Uses tables and rows | Uses collections and documents |
Fixed schema | Dynamic schema (flexible) |
Relationships using joins | Embedded documents or references |
Good for structured data | Great for semi-structured or unstructured data |
Q: If you’re building an application where different users have different fields in their profile, would SQL or MongoDB be easier to work with?
A: MongoDB! Because you don’t need to predefine a schema. One document can have a name and age, another might have name, age, and hobbies — and that’s totally okay.
In SQL:
CREATE TABLE users (
id INT,
name VARCHAR(50),
age INT
);
INSERT INTO users VALUES (1, 'Alice', 25);
In MongoDB:
db.users.insertOne({
_id: 1,
name: "Alice",
age: 25
});
{ acknowledged: true, insertedId: 1 }
Explanation: MongoDB stores data as documents
in collections
. There's no need to define the structure before inserting data. It's simple and fast.
You can insert different shaped documents into the same collection:
db.users.insertMany([
{ name: "Bob", age: 30 },
{ name: "Charlie", hobbies: ["chess", "cycling"], location: "India" }
]);
{ acknowledged: true, insertedIds: [ObjectId("..."), ObjectId("...")] }
Explanation: Notice how the second document has completely different fields? MongoDB allows that flexibility, which is powerful in agile development environments.
Q: Is it possible to store nested objects or arrays in SQL tables directly?
A: Not easily. SQL would need foreign keys and joins. MongoDB can directly store nested arrays and objects. This is useful for storing things like a user's multiple addresses or product reviews inside a single document.
db.products.insertOne({
name: "Laptop",
price: 70000,
specs: {
brand: "Dell",
ram: "16GB",
storage: "512GB SSD"
}
});
{ acknowledged: true, insertedId: ObjectId("...") }
Explanation: The specs
field is a sub-document (nested object). MongoDB handles this natively without additional joins or relations.
MongoDB is a flexible, document-based NoSQL database perfect for modern applications with semi-structured data. It allows fast development, easy scaling, and natural data modeling using embedded documents and arrays.
Now that you know what MongoDB is and how it differs from SQL, let’s move on to installing MongoDB on your system and setting up MongoDB Compass.
⬅ Previous Topic
MongoDB Course IndexNext Topic ⮕
MongoDB Use-cases and Features OverviewYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.