Mongodb Indexing and Aggregation in Mongodb
Mongodb Indexing and Aggregation in Mongodb
Note: The material to prepare this presentation has been taken from internet and are
generated only for students reference and not for commercial use.
Outline
Indexing
Aggregation
Indexing
•Indexes support the
efficient execution of
queries in MongoDB.
Indexing Types
• A single field index only includes data from a single field of the
Single Field Indexes documents in a collection.
Using CreateIndex
• db.CollectionName.createIndex( { KeyName: 1 or -1})
Using ensureIndex
• db.CollectionName.ensureIndex({KeyName: 1 or -1})
Using CreateIndex
• Single: db.stud.createIndex( { zipcode: 1})
• Compound: db.stud.createIndex( { dob: 1, zipcode: -1 } )
• Unique: db.stud.createIndex( { rollno: 1 }, { unique: true } )
• Sparse: db.stud.createIndex( { age: 1 }, { sparse: true } )
Using ensureIndex
• Single: db.stud.ensureIndex({“name":1})
• Compound: db.stud.ensureIndex ({“address":1,“name":-1})
Index Display
db.collection.getIndexes()
• Returns an array that holds a list of
documents that identify and describe the
existing indexes on the collection.
db.collection.getIndexStats()
• Displays a human-readable summary of aggregated
statistics about an index’s B-tree data structure.
• db.<collection>.getIndexStats( { index : "<index name>" } )
Index Drop
Syntax
• db.collection.dropIndex()
• db.collection.dropIndex(index)
Example
• db.stud.dropIndex()
• db.stud.dropIndex( { “name" : 1 } )
Indexing and Querying
• create an ascending index on the field name for a collection
records:
db.records.createIndex( { name: 1 } )
• This index can support an ascending sort on name :
db.records.find().sort( { name: 1 } )
• The index can also support descending sort
db.records.find().sort( { a: -1 } )
Indexing and Querying
db.stud.findOne( {rno:2} ), using index {rno:1}
db.c.find().sort( {name:1,age:1} ),
using index {name:1,age:1}
Indexing
Aggregation
Aggregation
Aggregations operations process data records and return computed
results.
Syntax:
• >db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)
Aggregation
• MongoDB’s aggregation framework is modeled on the concept
of data processing pipelines.
• Documents enter a multi-stage pipeline that transforms the
documents into an aggregated result.
• Other pipeline operations provide tools for grouping and
sorting documents by specific field or fields.
• In addition, pipeline stages can use operators for tasks such as
calculating the average or concatenating a string.
aggregate() method
Expression Description
Sums up the defined value from all documents in the
$sum
collection.
Calculates the average of all given values from all
$avg
documents in the collection.
Gets the minimum of the corresponding values from all
$min
documents in the collection.
Gets the maximum of the corresponding values from all
$max
documents in the collection.
Gets the first document from the source documents
$first
according to the grouping.
Gets the last document from the source documents
$last
according to the grouping.
Pipeline Concept
db.student.aggregate
([{$group : {_id : "$subject",
marks : {$min : "$marks"}}}]);
db.student.aggregate
([{$group : {_id : "$subject",
marks : {$max : "$marks"}}}]);
db.student.aggregate
([{$group : {_id : "$subject",
marks : {$avg : "$marks"}}}]);
db.student.aggregate
([{$group : {_id : "$subject",
marks : {$first : "$marks"}}}]);
LAST()
db.student.aggregate
([{$group : {_id : "$subject",
marks : {$last : "$marks"}}}]);
SUM()-Example 1
db.student.aggregate
([{$group : {_id : "$subject",
marks : {$sum : "$marks"}}}]);
db.student.aggregate
([{ $match: {subject:"OSD"}}])
db.student.aggregate
([{ $match: {subject:"OSD"}},
{$group:{_id:null,count:{$sum:1}}}]);
SUM()- Example 3
db.student.aggregate
([{ $match: {subject:"OSD"}},
{$group:{_id:null,count:{$sum:1}}}]);
db.student.aggregate
([{ $match: {subject:"OSD"}},
{$limit:1}]);
db.student.aggregate
([{ $match: {subject:"OSD"}},
{$skip:1}]);
Sort()
db.student.aggregate
([{ $match: {subject:"OSD"}},
{$sort:{marks:-1}}]);
db.student.aggregate
([{ $match: {subject:"OSD"}},
{$sort:{marks:1}}]);
Unwind()
• db.student.insert({rollno:9,name:"Anavi",marks:[80,30,50]});
• db.student.aggregate([{$unwind:"$marks"}])
Refernces
• https://www.tutorialspoint.com/mongodb/mongodb_aggrega
tion.htm
• http://pradipshewale.blogspot.in/2015/09/aggregation-and-
indexing-with-suitable.html
• https://www.infoq.com/articles/implementing-aggregation-
functions-in-mongodb
• http://docs.mongodb.org/manual/reference/operator/aggreg
ation/