MERN Stack Document
MERN Stack Document
1. Concepts :-
Core concepts:
● Collection: Equivalent to a table in SQL.
● Document: Equivalent to a row in SQL, stored as JSON-like objects.
● Field: Similar to columns in SQL.
● Indexing: Enhances query performance by creating indexes on fields.
2. Scaling :-
● Vertical Scaling: Increasing the hardware capacity of a single server
(e.g., more CPU, RAM).
● Horizontal Scaling: Distributing data across multiple servers using
sharding.
○ MongoDB supports horizontal scaling with built-in sharding.
○ Data is divided into smaller chunks based on a shard key.
3. SQL and Complex Transactions:-
● Unlike SQL databases, MongoDB supports:
○ No fixed schema.
○ Documents with nested structures.
● Transactions:
○ MongoDB supports multi-document transactions since version
4.0.
○ Allows ACID-compliant operations on multiple collections.
4. Documents Overview :-
● MongoDB uses documents to store data.
● Example:
{
"age": 29,
"address": {
"zip": "94107"
● Advantages:
○ Flexibility to store nested data.
○ Schema-less design.
5. Installing MongoDB (Windows) :-
● Download MongoDB from the official website.
● Install by running the .msi file.
● Configure the environment variable for the bin directory.
Start MongoDB:
● Install MongoDB:
mongo --version
7. JSON :-
● MongoDB stores data in BSON format, a binary-encoded version of
JSON.
● Example JSON document
{
"id": 1,
"name": "Alice",
8. Dynamic Schema :-
9. Mongoimport :-
● Example:
● Operators:
○ $eq: Equal
○ $gt: Greater than
○ $lt: Less than
12. Query Language: Projection :-
● Controls which fields are returned in the query result.
● Syntax:-
● Example:-
15. Sorting :-
● MongoDB allows sorting query results using sort().
● Example:-
db.users.find().sort({ age: 1 }); // Ascending order db.users.find().sort({
age: -1 }); // Descending order
16. Query Language: Cursors :-
● Cursors can be manipulated using methods:
○ limit(n): Limits the number of documents returned.
○ skip(n): Skips the first n documents.
○ Example:-
db.users.find().limit(5).skip(10);
1. Insertion :-
● Insert a Single Document:
db.collection.insertOne({ name: "Alice", age: 25, city: "New York" });
● Insert Multiple Documents:
db.collection.insertMany([
]);
2. Update :-
● Update a Single Document:
db.collection.updateOne( { name: "Alice" }, // Filter
);
3. save() Command :-
● Saves a document:
○ If the _id field exists, it updates the document.
○ If _id does not exist, it inserts the document.
● Example
db.collection.save({ _id: 1, name: "Alice", age: 27 });
5. Removing Documents:-
● Remove Single Document:-
db.collection.updateMany(
{ status: "Inactive" },
);
7. Upsert :-
● Combines Update and Insert. If no document matches the query, a new document
is created
);
8. Wire Protocol :-
● MongoDB's Wire Protocol handles communication between the
MongoDB server and the client.
● It is a low-level binary protocol.
● Enables operations like query execution, updates, and administrative
commands.
bulk.execute();
11. runCommand() :-
● Executes a MongoDB command.
● Syntax:
db.runCommand({ commandName: value });
● Example:-
db.runCommand({ collStats: "users" }); // Get stats for the "users" collection
12. isMaster() :-
● Checks if the current server is the primary in a replica set.
● Example:-
db.runCommand({ isMaster: 1 });
13. serverStatus() :-
● Provides an overview of the server's current status.
● Includes metrics like memory usage, connections, and operations.
● Example:-
db.killOp(opId);
db.collection.stats();
db.collection.drop();