Tuesday, June 30, 2015

Aggregation in Mongo Db



Aggregation Mappers
SQL
Mongo
Where
$match
GroupBy
$group
Having
$match
Select
$project
orderBy
$sort
Limit
$limit
Sum
$sum

Summerization of data
Do not store data in any data tables
Do not change data
Like group by clause


db.products.aggregate({$group:{_id:"$Category",TotalValue:{$sum:"$Price"}}})
"_id" : "Desktops", "TotalValue" : 0 }
"_id" : "Laptops", "TotalValue" : 0 }
db.products.aggregate({$group:{_id:"$Category",TotalValue:{$sum:"$Price"}}})
"_id" : "Laptops", "TotalValue" : 110000 }
db.products.aggregate({$group:{_id:"$Category",TotalValue:{$sum:"$Price"}}})
"_id" : "Desktops", "TotalValue" : 20000 }
"_id" : "Laptops", "TotalValue" : 110000 }

2 comments:

  1. for order by we can use like this :

    db.PublishedDocuments.find({ "$query" : { }, "$orderby" : { "Year" : -1, "Month" : -1 } }).limit(50);

    ReplyDelete
    Replies
    1. Yes we can use . Thanks for sharing . orderby or sort either of them can be used

      Delete