0% found this document useful (0 votes)
43 views10 pages

MongoDB Crash Course Essentials and Code Snippets

MongoDB-Crash-Course-Essentials-and-Code-Snippets yes
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)
43 views10 pages

MongoDB Crash Course Essentials and Code Snippets

MongoDB-Crash-Course-Essentials-and-Code-Snippets yes
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/ 10

Mo goDB Cra Cour e:

E e tial Code S ippet


This presentation provides a concise, "one-page" crash course on MongoDB,
covering its core concepts, essential operations, and practical code snippets in
Shell, Python (PyMongo), and Node.js (Mongoose). It's designed for quick
reference, allowing you to skim headers and copy what you need.
Core Co cept of Mo goDB

Docu e t DB Flexible Sc e a
MongoDB stores data in JSON-like documents (BSON), Unlike traditional relational databases, each document in a
organised within collections which reside inside databases. collection can have different fields. While flexible, you can still
This flexible structure allows for rich, hierarchical data enforce data integrity and rules using validators to maintain
representation. consistency.

I dexe Aggregatio
Indexes are crucial for optimising query performance. They The aggregation pipeline is a powerful framework for
should always be designed based on your application's specific performing advanced analytics and data transformations. It
read patterns to ensure efficient data retrieval. allows for complex data processing, including grouping, filtering,
and joining data.

Tra actio Horizo tal Scale


MongoDB supports multi-document ACID transactions, ensuring MongoDB achieves high availability (HA) through replication
data consistency and integrity across multiple operations. This and scales horizontally using sharding. Replication provides
feature requires a replica set or Atlas deployment. data redundancy and automatic failover, while sharding
distributes data across multiple servers for increased capacity.
Quick Start Data Model Exa ple
Ru Locally (Docker) Data Model Exa ple: S op Databa e
To quickly get a MongoDB instance running on your local machine, We'll use a 'shop' database with 'products', 'orders', and 'customers'
you can use Docker: collections to illustrate data modelling and operations. Here's how to
insert initial data:
docker run -d --name mongo -p 27017:27017 -e
MONGO_INITDB_ROOT_USERNAME=root -e // MongoDB Shell (mongosh)
MONGO_INITDB_ROOT_PASSWORD=pass mongo:7 use shop
db.products.insertMany([

Co ect (Mo goDB S ell/Co pa ) {_id: ObjectId(), sku: "BK-100", name: "Book", price: 399, tags:
["education","paper"], stock: 42},
Once running, connect using the MongoDB Shell (mongosh) or {_id: ObjectId(), sku: "PN-200", name: "Pen", price: 49, tags:
Compass with the following URI: ["stationery"], stock: 500}
])
mongodb://root:pass@localhost:27017/?authSource=admin db.customers.insertOne({
name: "Asha", email: "[email protected]", addresses: [{line1:"12
MG Rd", city:"Bengaluru", pin:"560001"}], createdAt: new Date()
})
db.orders.insertOne({
customerEmail: "[email protected]", items: [{sku:"BK-100",
qty:1, price:399}, {sku:"PN-200", qty:2, price:49}], total: 399 + 2*49,
status: "PLACED", createdAt: new Date()
})
CRUD E e tial ( o go )
Mastering Create, Read, Update, and Delete (CRUD) operations is fundamental to working with MongoDB. Here are common commands:

Read Operatio Array Operatio


MongoDB provides powerful operators for manipulating arrays
// READ (find)
within documents:
db.products.find({ price: { $lt: 100 } }, { name:1, price:1, _id:0 })

// ARRAY ops
// READ one
db.orders.updateOne(
db.products.findOne({ sku: "BK-100" })
{ status:"PLACED" },
{ $push: { items: { sku:"PN-200", qty:1, price:49 } }, $inc: { total:49
Create Operatio
}}
)
// CREATE
db.products.insertOne({ sku:"ER-300", name:"Eraser", price:20,
Delete Operatio
stock:1000 })
Remove documents from a collection based on specified criteria:
Update Operatio
// DELETE
// UPDATE (single field) db.products.deleteMany({ stock: { $lte: 0 } })
db.products.updateOne({ sku:"BK-100" }, { $set: { stock: 40 } })
These snippets cover the most common CRUD operations, providing
// UPSERT (create if missing) a solid foundation for managing your data in MongoDB.
db.products.updateOne({ sku:"NB-400" }, { $set:{
name:"Notebook", price:99, stock:200 } }, { upsert:true })
Query Patter Operator
MongoDB offers a rich set of query operators to filter and retrieve data precisely. Understanding these patterns is key to effective data access.

Co pari o Operator Projectio , Sort, Li it


Use these for numerical and string comparisons: Control which fields are returned, the order of results, and the
number of documents:
// Comparisons: $lt, $lte, $gt, $gte, $ne, $in, $nin
db.products.find({ tags: { $in: ["education"] }, price: { $gte: 100, // Projections & sort & limit
$lte: 500 } }) db.products.find({}, {name:1,
price:1}).sort({price:-1}).limit(5).skip(0)
Logical Operator
Di ti ct Value
Combine multiple query expressions:
Retrieve unique values for a specified field:
// Logical: $and, $or, $not
db.products.find({ $or: [{price:{$lt:50}}, {tags:"stationery"}] }) // Distinct
db.products.distinct("tags")
Regex (Regular Expre io )
These operators enable flexible and powerful querying, allowing you
For pattern matching in strings. Be cautious, as they can impact
to retrieve exactly the data you need from your MongoDB
performance if not anchored:
collections.

// Regex (be careful; not index-friendly unless anchored)


db.products.find({ name: /^Boo/ })
I dexi g for Perfor a ce
Indexes are critical for optimising query performance in MongoDB. Proper indexing can drastically reduce the time it takes to retrieve data.

Si gle-Field I dex TTL (Ti e-To-Live) I dex


Creates an index on a single field, often used for unique constraints: Automatically deletes documents from a collection after a specified
period:
// Single-field
db.products.createIndex({ sku: 1 }, { unique: true }) // TTL index (auto-delete after N seconds)
db.logs.createIndex({ createdAt: 1 }, { expireAfterSeconds: 3600 })
Co pou d I dex
Geo patial I dex
Indexes multiple fields. The order of fields matters, as leading fields
must be present in the query for the index to be used: Supports queries on geographical data, such as points, lines, and
polygons:
// Compound (order matters! leading fields must be in the query)
db.products.createIndex({ tags: 1, price: -1 }) // Geospatial
db.shops.createIndex({ location: "2dsphere" })
Text I dex db.shops.insertOne({ name:"Kiosk", location:{ type:"Point",
coordinates:[77.5946,12.9716] } })
Enables full-text search capabilities on string content: db.shops.find({ location: { $near: { $geometry:
{type:"Point",coordinates:[77.6,12.97]}, $maxDistance: 2000 } } })
// Text index (simple search)
db.products.createIndex({ name: "text", tags: "text" }) Careful index design is crucial for maintaining high performance as
db.products.find({ $text: { $search: "book stationery" } }) your data grows.
Aggregatio Pipeli e for A alytic
The aggregation pipeline is MongoDB's framework for performing advanced data processing, enabling powerful analytics and
transformations.

Sale by Product (fro order .ite ) Joi -like Lookup: Order to Cu to er


This pipeline calculates the total quantity and revenue for each The $lookup stage performs a left outer join to combine data from
product SKU from the 'orders' collection: two collections:

db.orders.aggregate([ db.orders.aggregate([
{ $unwind: "$items" }, { $lookup: { from:"customers", localField:"customerEmail",
{ $group: { _id: "$items.sku", qty: { $sum: "$items.qty" }, revenue: foreignField:"email", as:"cust" } },
{ $sum: { $multiply:["$items.qty","$items.price"] } } } }, { $set: { customer: { $first:"$cust" } } },
{ $sort: { revenue: -1 } }, { $project: { cust:0 } }
{ $limit: 5 } ])
])

Facet (Multiple Aggregatio i O e Pa )


The $facet stage allows you to run multiple aggregation pipelines on
the same set of input documents:

db.products.aggregate([
{ $facet: {
priceStats: [{ $group: { _id:null, min:{$min:"$price"}, max:
{$max:"$price"}, avg:{$avg:"$price"} } }],
tagCounts: [{ $unwind:"$tags" }, { $group:{ _id:"$tags", n:{$sum:1}
} }, { $sort:{n:-1} }]
}}
])
Tra actio Sc e a Validatio
Tra actio (Multi-Docu e t ACID) Sc e a Validatio
Transactions ensure that a series of operations are either all Schema validation allows you to enforce structure on your
completed successfully or all rolled back, maintaining data documents without losing MongoDB's inherent flexibility. It helps
consistency. This requires a replica set or Atlas deployment. catch bad data early.

const session = db.getMongo().startSession() db.createCollection("invoices", {


session.startTransaction() validator: {
try { $jsonSchema: {
const sdb = session.getDatabase("shop") bsonType: "object",
sdb.products.updateOne({ sku:"BK-100", stock:{ $gte:1 } }, { $inc:{ required: ["number","amount","createdAt"],
stock:-1 } }) properties: {
sdb.orders.insertOne({ customerEmail:"[email protected]", number: { bsonType:"string" },
items:[{sku:"BK-100", qty:1, price:399}], total:399, status:"PLACED", amount: { bsonType:"number", minimum:0 },
createdAt:new Date() }) createdAt: { bsonType:"date" },
await session.commitTransaction() paid: { bsonType:"bool" }
} catch (e) { }
await session.abortTransaction() }
throw e }
} finally { })
session.endSession()
}
C a ge Strea
Change streams allow applications to access real-time data changes
without polling the database. This is useful for building reactive
applications.

// In mongosh (needs replica set/Atlas)


const cs = db.orders.watch()
cs.on("change", ev => { printjson(ev) })
Relatio ip Productio Tip
Relatio ip : E bed v . Refere ce Co o Productio Tip
Choosing between embedding and referencing documents is a key Create compound indexes that align with your most frequent
data modelling decision: query filters and sort operations.
Prefer covered queries (projecting only indexed fields)
Embed: Use when relationships are 1-to-few, data is small, and
whenever possible to avoid accessing the actual documents.
typically read together (e.g., order items within an order
document). Avoid unbounded $regex and leading wildcards; consider using
text indexes or Atlas Search for efficient text searches.
Reference: Use for many-to-many relationships, large
documents, shared data, or when data is updated independently Implement schema validation to proactively identify and
(e.g., customer ID in orders, joined via $lookup). prevent the insertion of malformed data.
Keep individual documents under 16 MB. Be cautious with very
Backup, Replicatio , S ardi g large arrays; consider strategies like bucketing or pagination.

Replication: Ensures high availability with a primary and two or Leverage WiredTiger defaults for storage. Monitor your working
more secondary nodes, providing automatic failover. set vs. RAM to ensure efficient memory usage, and add indexes
judiciously.
Backups: Utilise Atlas snapshots for managed backups or
mongodump/mongorestore for simple data dumps. For write-heavy workloads, carefully consider sharding with a
well-distributed shard key (e.g., hashed _id or a field with high
Sharding: Distributes collections across multiple shards using a
cardinality).
shard key. Essential for scaling when a single server's resources
(RAM/IOPS) are insufficient.
De ig i g a Mo goDB Collectio : A Mi i C eckli t
Before implementing a new collection, follow this checklist to ensure optimal design and performance:

01 02

Defi e Top 3 Querie C oo e E bed v . Refere ce


Identify your most critical and frequent queries. This will guide your Decide whether to embed related data within documents or reference
indexing strategy and data model choices. them in separate collections, based on access patterns and data
relationships.

03 04

Draft I dexe Add Sc e a Validatio


Design indexes that efficiently support your identified queries, Implement schema validation rules for required fields to maintain
considering both filtering and sorting requirements. data quality and consistency.

05 06

Pla Arc ival/TTL E ure Replica Set for Tra actio


Determine a strategy for archiving or automatically deleting old data If multi-document consistency is required, confirm that your
using TTL indexes or custom archival processes. deployment is a replica set to support transactions.

By following this checklist, you can design robust and performant MongoDB collections tailored to your specific use case.

You might also like