MongoDB Aggregation 01 – $match $group $Sort

db.Persons.aggregate([
//stage1
{$match: {tags: {$size: 3}}}
])

db.Persons.aggregate([
//stage1
{$match: {isActive: true}}
])

db.Persons.aggregate([
//stage1
{$match: {age: {$gt: 6}}}
])


$group : Example 1

db.Persons.aggregate([
{$group: {_id: "$gender"}}
])
db.Persons.aggregate([
{$group: {_id: "$eyeColor"}}
])
/*OUTPUT :  1 */
{
    "_id" : "green"
}

/* 2 */
{
    "_id" : "brown"
}

/* 3 */
{
    "_id" : "blue"
}

$group by nested Fields : Example 1

{
    "_id" : ObjectId("5f1f23e002c633f9f578dcde"),
    "index" : 0,
    "name" : "Aurelia Gonzales",
    "isActive" : false,
    "registered" : ISODate("2015-02-11T04:22:39.000Z"),
    "age" : 20,
    "gender" : "female",
    "eyeColor" : "green",
    "favoriteFruit" : "banana",
    "company" : {
        "title" : "YURTURE",
        "email" : "aureliagonzales@yurture.com",
        "phone" : "+1 (940) 501-3963",
        "location" : {
            "country" : "USA",
            "address" : "694 Hewes Street"
        }
    },
    "tags" : [ 
        "enim", 
        "id", 
        "velit", 
        "ad", 
        "consequat"
    ]
}
db.Persons.aggregate([
{$group: {_id: "$company.location.country"}}
])
//OUTPUT :

{"_id" : "USA" }{ "_id" : "Italy" }{"_id" : "France" }{"_id" : "Germany"}

$group by multiple fields : Example 1

db.Persons.aggregate([
{$group: {_id: {
    eyeColor: "$eyeColor",
    favoriteFruit: "$favoriteFruit",
    age: "$age"
    }}}
])

Combine $match and $group Stages

db.Persons.aggregate([
{$match: {gender: "female"}},
{$group: {_id: {eyecolor: "$eyeColor", age: "$age", gender: "$gender"}}}
])

swap $match and $group

Filter From Group

db.Persons.aggregate([
{$group: {_id: {eyeColor: "$eyeColor", age: "$age"}}},
{$match: {"_id.eyeColor": "blue" }}
])
//OUTPUT : 
/* 1 */
{
    "_id" : {
        "eyeColor" : "blue",
        "age" : 38
    }
}
.....
.....

$Count

db.Persons.aggregate([
{$count: "allDocumentsCount"}
])
//OUTPUT :
/* 1 */
{
    "allDocumentsCount" : 1000
}


Here the count calculate different eyeColor

db.Persons.aggregate([
//stage 1
{$group: {_id: "$age"}},
//stage 2
{$count: "age"}
])
//OUTPUT: { "age" : 21 }
//2.
db.Persons.aggregate([
//stage 1
{$group: {_id: "$eyeColor"}},

//stage 2
{$count: "eyeColor"}
])
//OUTPUT : {"eyeColor" : 3}

Here two gender and three eyeColor, totally 6 combinations

db.Persons.aggregate([
//stage 1
{$group: {_id:{eyeColor: "$eyeColor", gender: "$gender"}}},

//stage 2
{$count: "eyeColorAndGender"}
])
//OUTPUT : { "eyeColorAndGender" : 6 }

//2. 21 ages and 3 eyecolor, totally : 63
db.Persons.aggregate([
//stage 1
{$group: {_id:{eyeColor: "$eyeColor", age: "$age"}}},

//stage 2
{$count: "eyeColorAndAge"}
])
//OUTPUT : { "eyeColorAndAge" : 63 }

//3. 
db.Persons.aggregate([
//stage 1
{$match: {age: {$gte: 25}}},

//stage 2
{$group: {_id:{eyeColor: "$eyeColor", age: "$age"}}},

//stage 3
{$count: "eyeColorAndAge"}
])
//OUTPUT : { "eyeColorAndAge" : 48 }


db.Persons.aggregate([
{$sort: {age: -1, gender: -1, eyeColor: 1}}
])

$group and $sort

db.Persons.aggregate([
{$group: {_id: "$age"}},
{$sort: {_id: 1}}
])
//OUTPUT : { "_id" : 20} {"_id" : 21}

//2.
db.Persons.aggregate([
{$group: {_id: "$eyeColor"}},
{$sort: {_id: 1}}
])
//OUTPUT : {"_id" : "blue"} {"_id" : "brown"} {"_id" : "green"}

Multiple Sort

db.Persons.aggregate([
{$group: {_id: {eyeColor : "$eyeColor", favoriteFruit: "$favoriteFruit" }}},
{$sort: {"_id.eyeColor": 1, "_id.favoriteFruit": 1}}
])
//OUTPUT: 
/* 1 */
{
    "_id" : {
        "eyeColor" : "blue",
        "favoriteFruit" : "apple"
    }
}

/* 2 */
{
    "_id" : {
        "eyeColor" : "blue",
        "favoriteFruit" : "banana"
    }
}

/* 3 */
{
    "_id" : {
        "eyeColor" : "blue",
        "favoriteFruit" : "strawberry"
    }
}

/* 4 */
{
    "_id" : {
        "eyeColor" : "brown",
        "favoriteFruit" : "apple"
    }
}
db.Persons.aggregate([
{$match: {eyeColor: {$ne: "blue"}}},
{$group: {_id: {eyeColor : "$eyeColor", favoriteFruit: "$favoriteFruit" }}},
{$sort: {"_id.eyeColor": 1, "_id.favoriteFruit": 1}}
])
//OUTPUT :
/* 1 */
{
    "_id" : {
        "eyeColor" : "brown",
        "favoriteFruit" : "apple"
    }
}

/* 2 */
{
    "_id" : {
        "eyeColor" : "brown",
        "favoriteFruit" : "banana"
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *