0% found this document useful (0 votes)
18 views

Advanced MongoDB

The document covers advanced MongoDB concepts including data modeling techniques such as 1:1, 1:Many, and Many:Many relationships, as well as indexing and aggregation operations. It highlights the importance of indexing for query performance and provides examples of aggregation pipelines and common operators used in aggregations. Key operators include $match, $group, $sort, and $lookup, which facilitate data filtering, grouping, and transformation.

Uploaded by

azamsyed811
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)
18 views

Advanced MongoDB

The document covers advanced MongoDB concepts including data modeling techniques such as 1:1, 1:Many, and Many:Many relationships, as well as indexing and aggregation operations. It highlights the importance of indexing for query performance and provides examples of aggregation pipelines and common operators used in aggregations. Key operators include $match, $group, $sort, and $lookup, which facilitate data filtering, grouping, and transformation.

Uploaded by

azamsyed811
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

Advanced Mongo DB

concepts

Reference: https://www.mongodb.com/docs/manual/
Data Modelling

• Relationship between documents


o 1:1 Embedded
o 1:Many Embedded
o 1:Many Reference
o Many:Many
1:1 Embedded Documents
Address belonging to a patron

Address belonging to a patron in


a embedded document
1:Many Embedded Documents
Address one and address two
belonging to a patron
1:Many Embedded Documents
Address one and address two
belonging to a patron embedded
in a single document
1:Many Reference Documents
Many books using the same
publisher
1:Many Embedded Documents
Using publisher as a reference
by keeping it in a separate
collection from books
1:Many Embedded Documents
Using publisher id to avoid
growing arrays
Many:Many Embedded Documents
Embedded documents to
represent many to many
Indexing in MongoDB

• If an appropriate index exists for a query, MongoDB uses the index to limit the number of
documents it must scan.
• Common Query pattern

• db.collection.getIndexes()

• db.collection.createIndex()

• db.collection.dropIndex()
Aggregation in MongoDB
• Aggregation operations process multiple documents and return computed results

• Aggregation Pipelines – consists of one or more stages that process document


Aggregation in MongoDB
• Data is filtered, sorted, grouped, transformed

db.collection.aggregate([
{$stage_name: { <expression> } },
{$stage_name: { <expression> } }
])

ORDER MATTERS! (limit your working dataset from one stage to


another)
Common operators used in Aggregations
• $match – filters for data that matches criteria

• $group – groups documents based on criteria

• $sort – puts the documents in a specified order

• $lookup – perform LEFT OUTER JOIN between two collections

• $out – save the results of an aggregation

• $cond – Evaluates a boolean expression to return one of the two specified return
expressions
Common operators used in Aggregations
• $addFields – add additional fields to documents

• $redact – remove fields from document

• Logical - $lt, $gt, $lte, $gte, $eq

• Mathematical - $sum, $avg, $cos

• Date/Time - $dayOfMonth, $dateFromString

You might also like