MongoDB Aggregate Query and Aggregation Pipeline Operator

mongoDB Aggregate Query with Example

MongoDB Aggregation Pipeline Operator

We will perform all the mongoDB Aggregation Operator on studentInfo DB. Create studentInfo Database in MongoDB server. Use the below code for creating the DB.

  

Insert Some Document in student collection where we can apply the MongoDB Collection Methods to explore further usage. Inserting five document in Student Collection. Below is the document list present in student collection.

{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf54"),
        "id" : 1,
        "name" : "M Kumar",
        "dob" : "2003-04-07",
        "marks" : 54,
        "created_at" : "2017-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf55"),
        "id" : 2,
        "name" : "MOHAMMED ISHAQ  H",
        "dob" : "2002-06-14",
        "marks" : 67,
        "created_at" : "2016-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf56"),
        "id" : 3,
        "name" : "ANBUSELVAM S",
        "dob" : "2004-06-18",
        "marks" : 87,
        "created_at" : "2019-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf57"),
        "id" : 4,
        "name" : "MARTINPRIYADOSS J",
        "dob" : "2004-03-08",
        "marks" : 43,
        "created_at" : "2012-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebf58376803d36568082c60"),
        "id" : 5,
        "name" : "JAGAN J",
        "dob" : "2003-04-10",
        "marks" : 98,
        "created_at" : "2012-11-13 23:33:42"
}

Aggregation Pipeline Stages


MongoDb Aggregation purpose is to process the data to get the computed Result. For executing that we use db.collectionName.aggregate() method.

Aggregation Syntax is     db.collectionName.aggregate( [ { <stage> }, ... ] )

$toLower() Aggregate Function


$toLower function in mongoDB convert the given strings in lower case and return the result. Below is the code for your Reference purpose.
db.student.aggregate([{$project :{name :{$toLower:"$name"}}}])
As above code will convert the document name field in lower case. Below is the output.
{ "_id" : ObjectId("5ebeaafd50845b5c0ea8bf54"), "name" : "m kumar" }
{ "_id" : ObjectId("5ebeaafd50845b5c0ea8bf55"), "name" : "mohammed ishaq  h" }
{ "_id" : ObjectId("5ebeaafd50845b5c0ea8bf56"), "name" : "anbuselvam s" }
{ "_id" : ObjectId("5ebeaafd50845b5c0ea8bf57"), "name" : "martinpriyadoss j" }
{ "_id" : ObjectId("5ebf58376803d36568082c60"), "name" : "jagan j" }


$toUpper() Aggregate Function


















No comments:

Post a Comment

Our Feature Post

There is a tree between houses of A and B If the tree leans on As House

    There is a tree between houses of A and B. If the tree There is a tree between houses of A and B. If the tree leans on A’s House, the t...

Our Popular Post