WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

MongoDB - Aggregation


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.

Syntax

>db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)

Example

In the collection you have the following data
{
    _id: ObjectId(~984y8_)e\mS=j8mc)
    title: 'MongoDB Overview',
    description: 'MongoDB is no sql database',
    by_user: 'tutorials point',
    url: 'http://www.welookups.com',
    tags: ['mongodb', 'database', 'NoSQL'],
    likes: 120
},
{
    _id: ObjectId(~984y8_)e\mS=j8md)
    title: 'NoSQL Overview',
    description: 'No sql database is very fast',
    by_user: 'tutorials point',
    url: 'http://www.welookups.com',
    tags: ['mongodb', 'database', 'NoSQL'],
    likes: 30
},
{
    _id: ObjectId(~984y8_)e\mS=j8me)
    title: 'Google Overview',
    description: 'Google is no sql database',
    by_user: 'Google',
    url: 'http://www.Google.com',
    tags: ['Google', 'database', 'NoSQL'],
    likes: 300
},

Now from the above collection, if you want to display a list stating how many tutorials are written by each user
     > db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : 1}}}])
{
    "result" : [
        {
            "_id" : "welookups",
            "num_tutorial" : 2
        },
        {
            "_id" : "Google",
            "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.
Expression Description Example
$sum Sums up the defined value from all documents in the collection. db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : "$likes"}}}])
$avg Calculates the average of all given values from all documents in the collection. db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$avg : "$likes"}}}])
$min Gets the minimum of the corresponding values from all documents in the collection. db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$min : "$likes"}}}])
$max Gets the maximum of the corresponding values from all documents in the collection. db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$max : "$likes"}}}])
$push Inserts the value to an array in the resulting document. db.mycol.aggregate([{$group : {_id : "$by_user", url : {$push: "$url"}}}])
$addToSet Inserts the value to an array in the resulting document but does not create duplicates. db.mycol.aggregate([{$group : {_id : "$by_user", url : {$addToSet : "$url"}}}])
$first Gets the first document from the source documents according to the grouping. Typically this makes only sense together with some previously applied “$sort”-stage. db.mycol.aggregate([{$group : {_id : "$by_user", first_url : {$first : "$url"}}}])
$last Gets the last document from the source documents according to the grouping. Typically this makes only sense together with some previously applied “$sort”-stage. db.mycol.aggregate([{$group : {_id : "$by_user", last_url : {$last : "$url"}}}])




Welookups is optimized for learning.Copy right 2018 kumar aditya singh .
All Right Reserved and you agree to have read and accepted our term and condition.
All Rights Reserved.