Вы находитесь на странице: 1из 8

AGGREGATION

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. MongoDB provides three ways to
perform aggregation: the aggregation pipeline, the map-reduce function, and single purpose
aggregation methods.
2.1 COUNT
How do you get the number of Debit and Credit transactions? One way to do it is by using
count() function as below.
COMMAND
> db.transactions.count({cr_dr : "D"});
or
> db.transactions.find({cr_dr : "D"}).length();
But what if you do not know the possible values of cr_dr upfront. Here Aggregation framework
comes to play. Seethe below Aggregate query.
COMMAND
> db.transactions.aggregate(
[
{
$group : {
_id : '$cr_dr', // group by type of transaction
// Add 1 for each document to the count for this type of transaction
count : {$sum : 1}
}
}
]
);
OUTPUT
{
"_id" : "C",
"count" : 3
}
{
"_id" : "D",
"count" : 5
}
2.2 SUM
How to get the summation of amount? See the below aggregate query.
COMMAND;
> db.transactions.aggregate(
[
{
$group : {
_id : '$cr_dr',
count : {$sum : 1}, //counts the number
totalAmount : {$sum : '$amount'} //sums the amount
}
}
]
);
OUTPUT
{
"_id" : "C",
"count" : 3.0,
"totalAmount" : 120.0
}
{
"_id" : "D",
"count" : 5.0,
"totalAmount" : 410.0
}
Another version that sums amount and fee.
COMMAND
> db.transactions.aggregate(
[
{
$group : {
_id : '$cr_dr',
count : {$sum : 1},
totalAmount : {$sum : { $sum : ['$amount', '$fee']}}
}
}
]
);
OUTPUT
{
"_id" : "C",
"count" : 3.0,
"totalAmount" : 128.0
}
{
"_id" : "D",
"count" : 5.0,
"totalAmount" : 422.0
}
2.3: AVERAGE
How to get the average amount of debit and credit transactions?

COMMAND
> db.transactions.aggregate(
[
{
$group : {
_id : '$cr_dr', // group by type of transaction (debit or credit)
count : {$sum : 1}, // number of transaction for each type
totalAmount : {$sum : { $sum : ['$amount', '$fee']}}, // sum
averageAmount : {$avg : { $sum : ['$amount', '$fee']}} // average
}
}
]
)
OUTPUT
{
"_id" : "C", // Amounts for credit transactions
"Count" : 3.0,
"Total Amount" : 128.0,
"Average Amount" : 40.0
}
{
"_id”: "D", // Amounts for debit transactions
"Count" : 5.0,
"total Amount" : 422.0,
"average Amount" : 82.0
}

RESULT
Thus the collection and aggregation commands is implemented successfully for a transaction.
EX NO:9

DATE: APPLICATION ENGINEERING

AIM:
To connect Mongodb with Java using Eclipse by adding jar file of mongo
driver file.

PROGRAM:

package mongodb;

import com.mongodb.MongoClient;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;

import java.net.UnknownHostException;

import com.mongodb.DB;

public class MongoClass {

public static void main(String[] args) {


try {
MongoClient mongoClient = new
MongoClient("localhost",27101);
System.out.print("Connection established successfully");
DB db=mongoClient.getDB("Admin");
DBCollection coll=db.getCollection("BalanceSheet");
DBCursor cursor=coll.find();

while(cursor.hasNext())
{
int i= 1;
System.out.println(cursor.next());
i++;

}
catch(UnknownHostException e) {
e.printStackTrace();
}

OUTPUT:
ERROR OCCURS WHEN UNKNOWN LOCAL HOSTI GIVEN
RESULT:
Thus the connection has been successfully done with Mongodb and java
using Eclipse with the help of Mongo java driver.

Вам также может понравиться