Monday, 27 January 2014

MongoDB Aggregate

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.

SYNTAX:

Basic syntax of aggregate() method is as follows
>db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)

EXAMPLE:



> db.democol.find().pretty()
{
        "_id" : ObjectId("52963c2a6f63f810a98b7a98"),
        "title" : "Learn mongo",
        "by" : "mani",
        "likes" : 100
}
{
        "_id" : ObjectId("52963ce96f63f810a98b7a99"),
        "by" : "subramanian",
        "comments" : [
                {
                        "user" : "Tiara",
                        "message" : "Worth to read"
                }
        ],
        "likes" : 20,
        "title" : "Learn SQL"
}
{
        "_id" : ObjectId("52a62f01fdaa40a1db551d04"),
        "title" : "Learn HTML5",
        "by" : "KGSM",
        "likes" : 240
}
{
        "_id" : ObjectId("52a62f3efdaa40a1db551d05"),
        "title" : "Learn Java",
        "by" : "Faulkner",
        "likes" : 209
}
{
        "_id" : ObjectId("52a62f61fdaa40a1db551d06"),
        "title" : "Basics of Life",
        "by" : "Peter",
        "likes" : 218
}
{
        "_id" : ObjectId("52a65a92fdaa40a1db551d07"),
        "title" : "Basics of Life",
        "by" : "Peter",
        "likes" : 318
}


1.

> db.democol.aggregate([{$group: {_id:"$by",likes:{$sum:"$likes"}}}])
{
        "result" : [
                {
                        "_id" : "Peter",
                        "likes" : 536
                },
                {
                        "_id" : "Faulkner",
                        "likes" : 209
                },
                {
                        "_id" : "KGSM",
                        "likes" : 240
                },
                {
                        "_id" : "subramanian",
                        "likes" : 20
                },
                {
                        "_id" : "mani",
                        "likes" : 100
                }
        ],
        "ok" : 1
}

2.

> db.democol.aggregate([{$group: {_id:"$by",likes:{$min:"$likes"}}}])
{
        "result" : [
                {
                        "_id" : "Peter",
                        "likes" : 218
                },
                {
                        "_id" : "Faulkner",
                        "likes" : 209
                },
                {
                        "_id" : "KGSM",
                        "likes" : 240
                },
                {
                        "_id" : "subramanian",
                        "likes" : 20
                },
                {
                        "_id" : "mani",
                        "likes" : 100
                }
        ],
        "ok" : 1
}

3.

> db.democol.aggregate([{$group: {_id:"$by",likes:{$max:"$likes"}}}])
{
        "result" : [
                {
                        "_id" : "Peter",
                        "likes" : 318
                },
                {
                        "_id" : "Faulkner",
                        "likes" : 209
                },
                {
                        "_id" : "KGSM",
                        "likes" : 240
                },
                {
                        "_id" : "subramanian",
                        "likes" : 20
                },
                {
                        "_id" : "mani",
                        "likes" : 100
                }
        ],
        "ok" : 1
}

ExpressionDescriptionExample
$sumSums up the defined value from all documents in the collection.db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : "$likes"}}}])
$avgCalculates the average of all given values from all documents in the collection.db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$avg : "$likes"}}}])
$minGets the minimum of the corresponding values from all documents in the collection.db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$min : "$likes"}}}])
$maxGets the maximum of the corresponding values from all documents in the collection.db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$max : "$likes"}}}])
$pushInserts the value to an array in the resulting document.db.mycol.aggregate([{$group : {_id : "$by_user", url : {$push: "$url"}}}])
$addToSetInserts the value to an array in the resulting document but does not create duplicates.db.mycol.aggregate([{$group : {_id : "$by_user", url : {$addToSet : "$url"}}}])
$firstGets 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"}}}])
$lastGets 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"}}}])

DBT - Models

Models are where your developers spend most of their time within a dbt environment. Models are primarily written as a select statement and ...