0% found this document useful (0 votes)
24 views14 pages

MERN Stack Document

The document outlines a comprehensive training course on the MERN stack, focusing on MongoDB, including its core concepts, scaling methods, and installation procedures for both Windows and Linux. It covers CRUD operations, query language basics, and administrative commands, providing examples for each operation. Additionally, it highlights the advantages of MongoDB's dynamic schema and BSON data storage format.

Uploaded by

Arnab Som
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views14 pages

MERN Stack Document

The document outlines a comprehensive training course on the MERN stack, focusing on MongoDB, including its core concepts, scaling methods, and installation procedures for both Windows and Linux. It covers CRUD operations, query language basics, and administrative commands, providing examples for each operation. Additionally, it highlights the advantages of MongoDB's dynamic schema and BSON data storage format.

Uploaded by

Arnab Som
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

MERN Stack Training Course

Section 1: Intro to MongoDB :-

1. Concepts :-

● MongoDB is a NoSQL database designed for high


availability, scalability, and performance.
● It stores data in BSON (Binary JSON) format, offering
flexibility compared to traditional relational databases.

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:
{

"name": "John Doe",

"age": 29,

"skills": ["Node.js", "MongoDB"],

"address": {

"city": "San Francisco",

"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:

● Run mongod in Command Prompt.

Access MongoDB shell:

● Use the mongo command.

6. Installing MongoDB (Linux) :-


● Add the MongoDB repository:

wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc |


sudo apt-key add -

echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu


focal/mongodb-org/6.0 multiverse" | sudo tee
/etc/apt/sources.list.d/mongodb-org-6.0.list

● Install MongoDB:

sudo apt-get update

sudo apt-get install -y mongodb-org


● Start MongoDB :

sudo systemctl start mongod

● Verify the installation:

mongo --version

7. JSON :-
● MongoDB stores data in BSON format, a binary-encoded version of
JSON.
● Example JSON document
{

"id": 1,

"name": "Alice",

"hobbies": ["reading", "cycling"]

8. Dynamic Schema :-

● Unlike SQL databases, MongoDB does not require a predefined


schema.
● Documents in the same collection can have different fields
● Example:
{ "name": "Alice", "age": 25 }
{ "name": "Bob", "skills": ["Java", "Python"] }

9. Mongoimport :-

● A tool for importing data into MongoDB.


● Usage:

mongoimport --db <database_name> --collection <collection_name>


--file <file_path> --jsonArray

● Example:

mongoimport --db test --collection users --file users.json --jsonArray

10. Cursors Introduction :-

● Cursor: A pointer to the result set of a query.


● Used for retrieving documents in MongoDB.
● Example:
var cursor = db.users.find();
while (cursor.hasNext()) {
printjson(cursor.next());
}

11. Query Language: Basic Concepts :-


● MongoDB queries are expressed as JSON objects.
● Example:-
db.collection.find({ field: value });

● Operators:
○ $eq: Equal
○ $gt: Greater than
○ $lt: Less than
12. Query Language: Projection :-
● Controls which fields are returned in the query result.
● Syntax:-

db.collection.find({ query }, { field1: 1, field2: 1 });

● Example:-

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

13. Query Language: Advantages of a Dynamic


Schema :-
● No predefined schema allows:
○ Faster prototyping.
○ Easier handling of changes in data structure.
○ Nested data representation.

14. Shell: Queries :-


● MongoDB provides an interactive shell for executing queries.
● Common commands:
○ show dbs: Lists databases.
○ use <database_name>: Switches to the specified database.
○ show collections: Lists collections in the current database.

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);

Section 2: CRUD and Admin Commands :-

1. Insertion :-
● Insert a Single Document:
db.collection.insertOne({ name: "Alice", age: 25, city: "New York" });
● Insert Multiple Documents:
db.collection.insertMany([

{ name: "Bob", age: 30 },

{ name: "Charlie", age: 28 }

]);

2. Update :-
● Update a Single Document:
db.collection.updateOne( { name: "Alice" }, // Filter

{ $set: { age: 26 } } // Update );


● Update Multiple Documents:
db.collection.updateMany(

{ city: "New York" }, // Filter

{ $set: { status: "Active" } } // Update

);

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 });

4. Partial Updates & Document Limits :-


● Partial Updates: Update specific fields without overwriting the
entire document

db.collection.updateOne({ name: "Alice" }, { $set: { city: "Boston" }


});
● Document Limits: A MongoDB document can be up to 16 MB in size.

5. Removing Documents:-
● Remove Single Document:-

db.collection.deleteOne({ name: "Alice" });

● Remove Multiple Documents:-

db.collection.deleteMany({ city: "New York" });


6. Multi Update :-
● Update multiple documents in one operation

db.collection.updateMany(

{ status: "Inactive" },

{ $set: { status: "Active" } }

);

7. Upsert :-
● Combines Update and Insert. If no document matches the query, a new document
is created

db.collection.updateOne( { name: "David" }, // Filter

{ $set: { age: 32 } }, // Update

{ upsert: true } // Option

);

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.

9. Bulk() Operations and Methods :-


● Perform multiple write operations in a single batch.
● Bulk API:-
var bulk = db.collection.initializeOrderedBulkOp();

bulk.insert({ name: "Eve", age: 22 });

bulk.find({ name: "Bob" }).updateOne({ $set: { age: 31 } });

bulk.execute();

● Unordered Bulk Operations (executed in parallel):

var bulk = db.collection.initializeUnorderedBulkOp();

10. Common Commands :-


● Basic Database Commands:
○ show dbs: List all databases.
○ show collections: List all collections in the current database.
○ db.createCollection("collectionName"): Create a new
collection.
● Data Manipulation Commands:
○ db.collection.find(): Retrieve documents.
○ db.collection.insertOne(): Insert a document.

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.runCommand({ serverStatus: 1 });

14. currentOp() & db.killOp() :-


● currentOp(): Displays currently running operations
db.currentOp();
● db.killOp(): Terminates a long-running operation.

db.killOp(opId);

15. stats() & collection.drop() :-


● stats(): Provides statistics for a specific collection

db.collection.stats();

● collection.drop(): Deletes an entire collection and its indexes.

db.collection.drop();

16. Review of Commands :-


● CRUD operations:
○ Insert: insertOne(), insertMany().
○ Read: find(), findOne().
○ Update: updateOne(), updateMany(), save().
○ Delete: deleteOne(), deleteMany().
● Administrative commands:
○ serverStatus(): Server metrics.
○ isMaster(): Replica set information.
○ runCommand(): Execute custom commands.
○ stats(): Collection statistics.

You might also like