Find and Manipulate Data using Aggregation in MongoDB

Kamran Ahmed
InstructorKamran Ahmed

Share this video with your friends

Send Tweet
Published 2 years ago
Updated a year ago

Aggregation in MongoDB is really powerful and allows you to create solid pipelines to extract and transform data. This is an introductory video that aims to demonstrate the basics of aggregation — we will see how to create some basic pipeline to extract data using $match and manipulate the output using $project stages.

The aggregation stages/operators covered in this video are:

You can see the list of all the available operators in the MongoDB docs

Kamran Ahmed: [00:00] Here I have a tweets collection with around 966 documents. In each document, we have the text of the tweet, the user who tweeted it.

[00:06] Then inside user, we have the screen_name of the user, the number of fields that they have, the number of favorite tweets, and then we have the number of followers they have, and the number of people that they're following.

[00:15] Let's say that we want to find only the tweets from the verified users. We can do that with the help of our aggregation framework, so I will do db.tweets.aggregate. It accepts a pipeline which is an area of transformations through which our data goes to make the end result.

[00:28] Here, I'm passing only the one transformation called match. Match has a similar format as find, so here I'm passing the query user.verified to be true. Now, if you run this query, you'll see that all the list aggregate has user [inaudible] to be verified.

[00:42] If you look at the results, you will see that it has a lot of data that we don't need. To get rid of all of this, we can add another transformation called project. Now, if we run this query, you will see that it will only bring us the users with only the fields we name. With the help of project, we can also rename the fields that we're getting in the result.

[00:59] Like you see here, our query is getting us a user object with screen_name inside that. Let's say that we want a user_name field with a string value instead, I can simply do that by replacing our projection with this. Here, I'm telling MongoDB that give me a user_name field and take its value from user.screen_name.

[01:14] Now, if you run this query, you will see that in the output we'll get the user_name as a string, instead of the object. Dollar sign in this statement is telling MongoDB to take it as a field of the document that we have, but not as a string.

[01:25] If we remove the dollar from here and we run this query again, you will see that the user name will become the user.screen_name, which is this one. If you put the dollar back here again, now it will take the values from our collection.

[01:35] We can also create dynamic fields. Let's say that we want to create a demi description from this user_name that we have. I'll put a new field called description that is created using concat of this with the user.screen_name. Now, if I run this query, I will get the user_name and also the description that is being prepared with this user_name.

[01:53] Aggregation in MongoDB is really powerful. It comes with a lot of building operators that you can see on the MongoDB documentation, which you can use to modify our old [inaudible] .

press0
press0
~ 2 years ago

egghead.io needs more stuff like this.

ps - wonder if it works on DocumentDB

Kamran Ahmed
Kamran Ahmedinstructor
~ 2 years ago

@press0 I haven't tried but it should work. They introduced MongoDB compatible aggregations earlier last year. Have a look at this article