WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

MongoDB - Aggregation


In SQL count(*) and with company with the aid of manner of is an equal of mongodb aggregation.

The mixture() Method

For the aggregation in MongoDB, you want to use aggregate() approach.

Syntax

>db.COLLECTION_NAME.Aggregate(AGGREGATE_OPERATION)

Example

In the collection you've got were given the subsequent data

    _id: ObjectId(~984y8_)emS=j8mc)
    name: 'MongoDB Overview',
    description: 'MongoDB isn't always any square database',
    by_user: 'tutorials factor',
    url: 'http://www.Welookups.Com',
    tags: ['mongodb', 'database', 'NoSQL'],
    likes: one hundred twenty
,

    _id: ObjectId(~984y8_)emS=j8md)
    name: 'NoSQL Overview',
    description: 'No sq. Database can be very speedy',
    by_user: 'tutorials point',
    url: 'http://www.Welookups.Com',
    tags: ['mongodb', 'database', 'NoSQL'],
    likes: 30
,

    _id: ObjectId(~984y8_)emS=j8me)
    name: 'Google Overview',
    description: 'Google is no square 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 mentioning how many tutorials are written through means of every user
     > db.Mycol.Mixture([$group : _id : "$by_user", num_tutorial : $sum : 1])

    "end end result" : [
        
            "_id" : "welookups",
            "num_tutorial" : 2
        ,
        
            "_id" : "Google",
            "num_tutorial" : 1
        
    ],
    "exact enough" : 1

>


Sql same query for the above use case could be pick by_user, count(*) from mycol organization with the useful resource of by_user.
Expression Description Example
$sum Sums up the defined charge from all files inside the collection. db.Mycol.Mixture([$group : _id : "$by_user", num_tutorial : $sum : "$likes"])
$avg Calculates the common of all given values from all documents inside the series. db.Mycol.Aggregate([$group : _id : "$by_user", num_tutorial : $avg : "$likes"])
$min Gets the minimum of the corresponding values from all documents within the series. db.Mycol.Combination([$group : _id : "$by_user", num_tutorial : $min : "$likes"])
$max Gets the maximum of the corresponding values from all files inside the series. db.Mycol.Mixture([$group : _id : "$by_user", num_tutorial : $max : "$likes"])
$push Inserts the price to an array inside the ensuing file. db.Mycol.Combination([$group : _id : "$by_user", url : $push: "$url"])
$addToSet Inserts the fee to an array in the resulting report but does now not create duplicates. db.Mycol.Combination([$group : _id : "$by_user", url : $addToSet : "$url"])
$first Gets the first document from the supply documents regular with the grouping. Typically this makes handiest experience collectively with some previously performed “$type”-degree. db.Mycol.Mixture([$group : _id : "$by_user", first_url : $first : "$url"])
$last Gets the very last file from the supply files in step with the grouping. Typically this makes only sense together with some formerly implemented “$type”-level. db.Mycol.Aggregate([$group : _id : "$by_user", last_url : $last : "$url"])




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