0% found this document useful (0 votes)
67 views7 pages

Aggregation and Indexing

Aggregation operations allow grouping and performing computations on document data to return aggregated results. The aggregate() method is used, taking a pipeline of aggregation stages like $group, $match, $project. For example, to count documents by author, the $group stage would group by the "by_user" field and sum the counts. Indexing supports efficient querying by creating indexes on fields and ordering results. The ensureIndex() method is used to create indexes, specifying the field(s) and sort order.
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)
67 views7 pages

Aggregation and Indexing

Aggregation operations allow grouping and performing computations on document data to return aggregated results. The aggregate() method is used, taking a pipeline of aggregation stages like $group, $match, $project. For example, to count documents by author, the $group stage would group by the "by_user" field and sum the counts. Indexing supports efficient querying by creating indexes on fields and ordering results. The ensureIndex() method is used to create indexes, specifying the field(s) and sort order.
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/ 7

Database Management System TE Computer Engineering (2018-

Assignment No.
11
Problem Statement : Implement aggregation and indexing with suitable example
using MongoDB.

Theory : MongoDB is an open-source document database and leading NoSQL


database. MongoDB is written in C++. This tutorial will give you great understanding on
MongoDB concepts needed to create and deploy a highly scalable and performance-
oriented database.

Aggregations operations process data records and return computed results. Aggregation operations
group values from multiple documents together, and can perform a variety of operations on the
grouped data to return a single result. In SQL count(*) and with group by is an equivalent of mongodb
aggregation.
The aggregate() Method For the aggregation in MongoDB, you should use

aggregate() method. Basic syntax of aggregate() method is as follows:

>db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)

Example

In the collection you have the following data:

_id:

ObjectId(7df78ad8902c)

title: 'MongoDB Overview',

description: 'MongoDB is no sql

database', by_user: 'tutorials point',

url: 'http://www.tutorialspoint.com',

tags: ['mongodb', 'database',

'NoSQL'], likes: 100

Department of Computer Engineering, SIT, Page 109


Database Management System TE Computer Engineering (2018-
},

_id: ObjectId(7df78ad8902d)

Department of Computer Engineering, SIT, Page 109


Database Management System TE Computer Engineering (2018-

title: 'NoSQL Overview',

description: 'No sql database is very

fast', by_user: 'tutorials point',

url: 'http://www.tutorialspoint.com',

tags: ['mongodb', 'database',

'NoSQL'], likes: 10

},

Now from the above collection, if you want to display a list stating how many tutorials are written by each
user, then you will use the following aggregate() method:

> db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum :1}}}])

"result" : [

"_id" : "tutorials point", "num_tutorial" : 2

},

{
"_id" : "Neo4j","num_tutorial" : 1

}],

"ok" : 1

}>

Sql equivalent query for the above use case will be select by_user, count(*) from mycol
group by by_user.

Department of Computer Engineering, SIT, Page 109


Database Management System TE Computer Engineering (2018-

Pipeline Concept

In UNIX command, shell pipeline means the possibility to execute an operation on some input
and use the output as the input for the next command and so on. MongoDB also supports
same concept in aggregation framework. There is a set of possible stages and each of those
is taken as a set of documents as an input and produces a resulting set of documents (or the
final resulting JSON document at the end of the pipeline). This can then in turn be used for the
next stage and so on.

Following are the possible stages in aggregation framework:


$project: Used to select some specific fields from a collection.

$match: This is a filtering operation and thus this can reduce the amount of documents that
are given as input to the next stage.

$group: This does the actual aggregation as discussed above.


Department of Computer Engineering, SIT, Page 109
Database Management System TE Computer Engineering (2018-
$sort: Sorts the documents.

$skip: With this, it is possible to skip forward in the list of documents for a given
amount of documents.
$limit: This limits the amount of docu ments to look at, by the given number starting
from the current positions.
$unwind: This is used to unwind document that are using arrays. When using an array, the
data is kind of pre-joined and this operation will be undone with this to have individual
documents again. Thus with this stage we will increase the amount of documents for the next
stage.

Indexes support the efficient resolution of queries. Without indexes, MongoDB must scan every
document of a collection to select those documents that match the query statement This scan is
highly inefficient and require MongoDB to process a large volume of data.

Indexes are special data structures, that store a small portion of the data set in an easy -to-
traverse form. The index stores the value of a specific field or set of fields, ordered by the
value of the field as specified in the index.

The ensureIndex() Method

To create an index you need to use ensureIndex() method of MongoDB. The basic syntax of
ensureIndex() method is as follows().

>db.COLLECTION_NAME.ensureIndex({KEY:1})

Here key is the name of the file on which you want to create index and 1 is for ascending
order. To create index in descending order you need to use -1.

Example

>db.mycol.ensureIndex({"title":1})

In ensureIndex() method you can pass multiple fields, to create index on multiple fields.

>db.mycol.ensureIndex({"title":1,"description":-1})

ensureIndex() method also accepts list of options (which are optional). Following is the list:

Department of Computer Engineering, SIT, Page 109


Database Management System TE Computer Engineering (2018-

Department of Computer Engineering, SIT, Page 109


Database Management System TE Computer Engineering (2018-

Conclusion: - Thus we have studied use and implementation of aggregation function &indexing
function.

Department of Computer Engineering, SIT, Page 109

You might also like