0% found this document useful (0 votes)
36 views31 pages

Adbms Unit 3

This document provides an overview of MongoDB's aggregation framework, detailing various operations such as $group, $limit, $project, $match, $addFields, $count, $lookup, and $out, along with their syntax and examples. It also discusses MongoDB indexing, including single field indexes, compound indexes, and partial indexes, explaining their purposes and how to implement them. The document emphasizes the importance of understanding data structure and query requirements for effective data analysis and retrieval in MongoDB.

Uploaded by

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

Adbms Unit 3

This document provides an overview of MongoDB's aggregation framework, detailing various operations such as $group, $limit, $project, $match, $addFields, $count, $lookup, and $out, along with their syntax and examples. It also discusses MongoDB indexing, including single field indexes, compound indexes, and partial indexes, explaining their purposes and how to implement them. The document emphasizes the importance of understanding data structure and query requirements for effective data analysis and retrieval in MongoDB.

Uploaded by

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

ADVANCED DATA BASE

MANAGEMENT
SYSTEM
UNIT-3
UNIT-III

• MongoDb Aggregation Frameworks and MongoDb


Aggregation operations:
$group, $limit, $project, $sort, $match, $add fields,
$count, $lookup, $out operators. MongoDb sorting,
• MongoDb Indexing:
Single field Indexes, Sorting with Indexes,
Compound Indexes, Partial Indexes.
MongoDB Aggregation Frameworks & Operations
 $group
 The $group command in MongoDB's aggregation framework is a powerful tool for performing complex data
analysis and summarization. It allows users to group documents based on specified keys and perform aggregate
functions on the grouped data.
 The $group command is an important stage in MongoDB's aggregation pipeline which enables users to
 Perform complex data analysis and summarization.
 Along with basic aggregate functions like sum, count, and average the $group supports a variety of other
operations such as finding the maximum or minimum value in a group, concatenating strings and
Calculating standard deviations
 Users can group documents by multiple keys, allowing for more Detailed analysis of data.
 The $group stage is followed by other pipeline stages like $match, $sort, and $project which enabling users to
perform comprehensive data transformations and analysis in a single pipeline.
 Using the $group command efficiently requires an understanding of the data structure and the specific
requirements of the analysis as improper grouping can lead to inaccurate results.
 Syntax:
 $group: { _id: <expression>, <field1>: { <accumulator1>: <expression1> }, ... } }
$_id: The field used to group documents. It can be an existing field or a computed expression.
<field1>, <field2>, etc.: Fields to include in the output.
<accumulator1>, <accumulator2>: Aggregate functions to apply to grouped data.
<expression>, <expression1>, etc.: Expressions to compute values for grouping or aggregation.
MongoDB Aggregation Frameworks & Operations
 $group- Example- counting the number of documents
MongoDB Aggregation Frameworks & Operations
 $group- Example- Retrieve distinct values
MongoDB Aggregation Frameworks & Operations
 $group- Example- Group by item having
MongoDB Aggregation Frameworks & Operations
 $group- Example- Calculate Count, Sum, and Average
MongoDB Aggregation Frameworks & Operations
 $group- Example- Group by null
MongoDB Aggregation Frameworks & Operations
 $limit
 The limit() method in MongoDB is a powerful tool used to control the number of documents returned in a query
result. It is particularly beneficial when working with large collections as it allows for the restriction of result set
sizes thereby improving performance and reducing client load.
 Limits the number of documents passed to the next stage in the pipeline.
 Syntax: { $limit: <positive 64-bit integer> }
 Example:
MongoDB Aggregation Frameworks & Operations
 $project
 The $project function in MongoDB passes along the documents with only the specified fields to the next stage in
the pipeline. This may be the existing fields from the input documents or newly computed fields.
 Syntax: { $project: { <specifications> } }
 The specification for $project command contain the inclusion of fields, the suppression of the _id field, the include
Specific Fields from Embedded Documents

Specification Description

<field>: <1 or true> Specify the inclusion of a field.

_id: <0 or false> Specify the suppression of the _id field.

<field>: include Specific Fields from Embedded Documents


<expression>
MongoDB Aggregation Frameworks & Operations
 $project- Inclusion of fields
MongoDB Aggregation Frameworks & Operations
 $project- suppression of the _id field
MongoDB Aggregation Frameworks & Operations
 $project- Include Specific Fields from Embedded Documents
MongoDB Aggregation Frameworks & Operations
 $match
 The MongoDB $match operator filters the documents to pass only those documents that match the specified
condition(s) to the next pipeline stage.
 Syntax: { $match: { <query> } }
 It is very much effective if you place the $match as early as possible in the aggregation pipeline that limits the total
number of documents in the aggregation pipeline.
 When $match placed at the very beginning of a pipeline, the query can take advantage of indexes.
 The $where can not be used in $match queries as part of the aggregation pipeline.
 Example:
MongoDB Aggregation Frameworks & Operations
 $match-Examples:
MongoDB Aggregation Frameworks & Operations
 $addfields- adds new fields to documents.
 The MongoDB $addfields operator adds the nee fields to existed documents.
 Syntax: { $addfields: { <query> }}
 Example:
MongoDB Aggregation Frameworks & Operations
 $count-
 The MongoDB $count operator counts the total amount of documents passed from the previous stage.
 Syntax: { $count: { <query> }}
 Example:
MongoDB Aggregation Frameworks & Operations
 $lookup-
 The MongoDB $count operator counts the total amount of documents passed from the previous stage.
 T$lookup operator in MongoDB allows us to perform a left outer join between documents from two collections.
This means we can perform the query of documents from one collection with data from another collection based
on a specified matching condition.
 It's useful for combining data from different collections for analysis or reporting purposes.
The operator can handle cases where there are no matching documents in the foreign collection, providing
flexibility in data retrieval.
 Syntax:{
$lookup: {
from: <foreignCollection>,
localField: <fieldInInputDocument>,
foreignField: <fieldInForeignDocument>,
as: <outputArrayField>}
}
from: The name of the foreign collection to join with.
localField: The field from the input documents that will be used for matching.
foreignField: The field from the foreign collection that will be used for matching.
as: The name of the output array field that will contain the joined documents.
MongoDB Aggregation Frameworks & Operations
 $lookup-
MongoDB Aggregation Frameworks & Operations
 $lookup-
MongoDB Aggregation Frameworks & Operations
$Out-
 In MongoDB Aggregation, $out is an aggregation pipeline stage that allows us to save the results of
an aggregation operation to a specified collection.
 It writes the output of the aggregation pipeline to a new or existing collection. This stage is useful when we want
to save the results of complex aggregations for later use or analysis without further processing in the pipeline.
 $out stage must be the last stage in the aggregation pipeline. This stage is beneficial when we want to persist the
aggregated data for further analysis and reporting or to serve as a basis for subsequent queries.
Syntax:
The syntax for the $out stage is given below:
{ $out: { db: "<output-db>", coll: "<output-collection>" } }
to write the results of an aggregation pipeline to a specified collection in a specified database.
$Out query is :db.c4.aggregate([{$group:{_id:"$name",stunum:{$sum:"$number"}}},{$out:"student_details“
Collections before after applying $out - (here db5 database considered from Mongo dB localhost)
}])
MongoDB Aggregation Frameworks & Operations
$Out-
$Out query1(project) db.c4.aggregate([{$project:{number:1,marks:1}},{$out:"student_details1"}])

}])
MongoDB Aggregation Frameworks & Operations
$Out-
$Out query1(group) :db.c4.aggregate([{$group:{_id:"$name",stunum:{$sum:"$number"}}},{$out:"student_details“

}])
MongoDB Aggregation Frameworks & Operations
 $sort- Mongo dB sort
 In MongoDB, the sort() method is used to specify the order in which the query returns matching documents from a
collection. It allows users to arrange documents in either ascending (1) or descending (-1) order based on the values
of one or more fields.
 MongoDB sort()
 The sort() method in MongoDB is used to specify the order in which the query returns matching documents from a
given collection. It must be applied to the cursor before retrieving any documents from the database.
 It takes a document as a parameter that contains a field: value pair that defines the sort order of the result set. The
value is 1 or -1 specifying an ascending or descending sort respectively.
 If a sort returns the same result every time we perform on the same data, then such type of sort is known as a stable
sort.
 If a sort returns a different result every time we perform on the same data, then such type of sort is known
as unstable sort.
 MongoDB generally performs a stable sort unless sorting on a field that holds duplicate values.
 limit() method with the sort() method, it will return the first m documents, where m is the given limit.
 MongoDB can find the result of the sort operation using indexes.
 If MongoDB does not find sort order using index scanning, then it uses the top-k sort algorithm.
 Syntax:
 db.Collection_Name.sort({field_name:1 or -1})
MongoDB Aggregation Frameworks & Operations
 $sort- Mongo dB sort- Examples(Ascending order sorting)
 db.c4.aggregate([{$sort:{name:1}}]) (or) db.c4.find().sort({number:1})
MongoDB Aggregation Frameworks & Operations
 $sort- Mongo dB sort- Examples (Descending Order sort)
 db.c4.aggregate([{$sort:{name:-1}}]) (or) db.c4.find().sort({number:-1})
MongoDb Indexing
 Indexing i.e., fetching the data in a short span of time without iterating over the whole dataset.
MongoDB is a NoSQL document type database that follows indexing.
 Indexes make searching in a collection is easier with the limited number of documents.
 A binary tree is the data structure used by an index. In documents, the _id field is a default
index which is automatically created by MongoDB and we are not allowed to drop this index.
 Types of index
 MongoDB provides different types of indexes that are used according to the data type or
queries. The indexes supported by MongoDB is are as follows:
1. Single field Index
2. Compound Index
3. Multikey Index
Mongo DB Indexing
 Single field Indexing – It means index on a single field of a document. This index is helpful for
fetching data in ascending as well as descending order.
 Syntax: db.collection.createIndex({“<fieldName>” : <1 or -1>});
 Example: Ascending order
db.c4.createIndex({number:1})
Output:number_1
db.c4.find().sort({number:1})
Output: same as sort method.
 Example: Decending order
db.c4.createIndex({number:-1})
Output:number_1
db.c4.find().sort({number:-1})
Output: same as sort method.
Mongo DB Indexing
 Compound Indexing –combine multiple fields for compound indexing and that will help for
searching or filtering documents in that way. Or in other words, the compound index is an
index where a single index structure holds multiple references.
 Syntax: db.<collection>.createIndex( { <field1>: <type>, <field2>: <type2>, … } )
 combine the required fields in this pattern. Also the value of these fields is 1(for ascending
order) or -1(for descending order).
 Note: Compound indexes may have a single hashed index field but a hashing function is
required by Hashed indexes to compute the hash of the value of the index field.
 Example: Ascending order
db.c4.createIndex({age:1,number:1})Output:age_1,number_1
db.c4.find().sort({age:1,number:1}) Output: same as sort method. If the two age fields are same
number then checks the next field for sorting.
 Example: Descending order
db.c4.createIndex({age:-1,number:-1})Output:age_1,number_1
db.c4.find().sort({age:-1,number:-1}) Output: same as sort method. If the two age fields are same
number then checks the next field for sorting.
 -1,1 and 1 and -1 are also allowable for the field numbers.
Mongo DB Indexing
 Partial Indexing –Partial indexes only index the documents in a collection that meet a
specified filter expression. By indexing a subset of the documents in a collection, partial
indexes have lower storage requirements and reduced performance costs for index creation and
maintenance.
 use the db.collection.createIndex() method with the partialFilterExpression option.
 The partialFilterExpression option accepts a document that specifies the filter condition using:
•$gt, $gte, $lt, $lte expressions,
•equality expressions (i.e. field: value or using the $eq operator),
•$exists: true expression,
•$type expressions,
•$and operator,
•$or operator,
•$in operator
)
Mongo DB Indexing
 Partial Indexing
 Example:
 db.c4.createIndex( { name: 1 }, { partialFilterExpression: { age: { $gt: 26} } }

You might also like