Sunday, September 15, 2019

Projection & Aggregation In MongoDB


Projection & Aggregation In MongoDB

Here we are going to explain How to use Projection and Aggregation in MongoDB.

Sometimes, we want to display only specific data from a collection(like table in MySQL). In MongoDB, if we use only find() then it gives all the documents of that collection. So, we use key names to display a specific key in collection. It takes Boolean value 0 or 1. Here, 1 means display and 0 means hide.



Here, Projection means to select necessary data rather than select whole data of a document.

For Example:

db.getCollection("student").find({}, {'name' : 1})

Result:

Json
{
"_id" : ObjectId("5c4f5481e71c8c1ef47e2f7e"),
"name" : "suresh kumar"
}
{
"_id" : ObjectId("5c4f5481e71c8c1ef47e2f7f"),
"name" : "mahesh verma"
}
{
"_id" : ObjectId("5c4f5481e71c8c1ef47e2f80"),
"name" : "gagandeep"
}
{
"_id" : ObjectId("5c4f5481e71c8c1ef47e2f81"),
"name" : "kamala"
}



Here, above query will gives only two things: one is name of all the students and other is _id, which is by default comes with this query. If we want to remove this _id key, then we have to use one more key _id with comma separated and value will be 0.

db.getCollection("student").find({}, {'name' : 1, '_id' : 0})

Now, above query will give all the names of collection student;
Result:

Json
{
"name" : "suresh kumar"
}
{
"name" : "mahesh verma"
}
{
"name" : "gagandeep"
}
{
"name" : "kamala"
}

In above query, we define name key 1; it means when we provide value 1 to a key, then all the keys of that collection will be automatically disabled means set to 0 except _id key. Suppose, we forgot this point and we defined one more field city as 0, then:

db.getCollection("student").find({}, {'name' : 1, 'city' : 0})

Now, above query will give error : Projection cannot have a mix of inclusion and exclusion. It means, only _id can define 0, because its default value is 1 in MongoDB.


Aggregation
Aggregation basically groups the data from multiple documents and operates in many ways on those grouped data in order to return one combined result. In SQL count(*) and with group by is an equivalent of MongoDB aggregation.

Aggregate function groups the records in a collection, and can be used to provide total number(sum), average, minimum, maximum etc out of the group selected.
Aggregations are a set of functions that allow you to manipulate the data being returned from a MongoDB query,
In MySQL, we use grouping(to find data grouped data), max(to find maximum), min(to find minimum), same as in MongoDB, we use aggregation to perform these operations.

Query 1:

db.getCollection("student").aggregate([{$group : {_id : "$city", MyResult : {'$sum' : 1}}}])

Result:

Json

{
"_id" : "Gurgaon",
"MyResult" : 2.0
}
{
"_id" : "Rewari",
"MyResult" : 1.0
}

Query 2:

db.getCollection("student").aggregate([{$group : {_id : "$state", MyResult : {'$max' : "$age"}}}])

Result:

Json
{
"_id" : "Harayana",
"MyResult" : "26"
}
{
"_id" : "UP",
"MyResult" : "28"
}

If you have any query, Please mail me phpeasysolutions@gmail.com.

No comments:

Post a Comment