Complete MongoDB aggregation pipeline course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there everyone it here back again with another video and welcome to this very special and unique video this video is aimed to make you an absolute professional in the world of mongodb aggregation pipeline so I'll walk you through what this is all about and it is so much for fun first of all who am I hi my name is zes I've been writing code for almost a decade and I've been teaching same uh for almost the same years I've done my fair share of startups successful ones and currently working at a unique uh company here in India now this tutorial is especially designed to cater the audience who are interested in learning the mongod DB aggregation so first and foremost I would like to address who is the target audience for this particular video this video is a not for absolute beginner uh for example if you are a beginner in mongodb world you have never built any application or you cannot even build a crud application in mongod DB you have no idea how to connect the mongod uh then sadly probably this is not the video for you this is not intended or you are not the primary audience for this video this video is for somebody who knows at least the basics of mongod DB not high-end but at least the basics that when I say you need a connection string from mongodb Atlas you know what that string is or how to get one also somebody who knows uh what are update one or update many or have done at least the crud operation in the mongodb but now wants to take their skills onto the next level using the mongodb aggregation pipelines which is one of the Fantastic thing you can Master if you are in the web development or remotely even minutely work with the mongodb so that's what the goal of this uh entire tutorial is uh one more disclaimer here I would like to mention that we are going to be going through from absolute scratch uh so you don't have to worry that how the setup and everything will work I'll give you uh that entirely uh the disclaimer is that the data that I'm using in this entire video uh first of all the data doesn't really matter you can work with any data that you have already that is also AB absolutely fine but the data that I'm using in this video I don't own that data half of it is generated by chat GPT and half half of it is taken from the Kel as well uh but regardless the data is not the primary point of this video it can work with any data what we'll be learning is the scenario based question so yes I have specifically prepared 20 or 20ish questions for you and while learning how to grab the answers of that questions you'll be learning aggregation pipeline these are all real world situations and simulation that will help you to properly understand the mongodb aggregation pipeline what it is how it works and where you can use it you will get all of that in this particular video so with that I think all of the situation is clear to us I would like to welcome all of you in this uh great journey the video is little bit informal I'll be sipping up my water in between so please don't mind me there're doing that and together I highly recommend to follow along with me this is so much of fun to work with it and one thing I can promise is by the end of this video you'll be having your Mastery with bongod DB aggregation pipeline all right with that let's get started how things are going to be rolling it out let me share the screen with you so first of all uh let's go on to the official documentation of what does the mongodb aggregation pipeline says and how it works so this aggregation pipeline actually makes mongodb one of the most powerful uh databases among the all so let me walk you through what this is so the aggregation pipeline consists of one or more stages so we'll focus on how to build these stages and what it basically does there are information in your database which needs to be calculated evaluated grouped or something like that or can be filtered but these are not directly there but with the help of aggregation pipeline you can find the answers of those questions and mongodb will will do all the operation for you like grouping the documentation filtering the documentation calculating the values averages and all these things we will learn about these pipelines as well as well as we will also learn about the operators in the mongodb uh something like count how to find averages and a lot more than that so we'll go through one by one of how these actually works and this is one of the most powerful thing that you can master that uh this is how the aggregation pipeline looks like so this is what we'll be doing let's just say we have an orders database in which we want to perform these aggregate operation this is how you'll be WR writing the code the entire tutorial is agnostic of any programming language that you might be using so you might be using python or JavaScript or Ruby doesn't really matter I'll keep it absolutely code agnostic so this is the array and inside this array we write objects and each object act as a stage so whatever the data you collect in one stage uh act as a source for the second stage and so on so forth you can have 2 3 four how many stages you would like to have uh you can just work with that we'll see exactly how this actually works we'll have a discussions and all these things so this is basically our stage our stage for setting all of this now how we are going to work with this let's set up a custom environment for us and for this we need some data I do have the data and the best thing is we can do all of this in the vs code itself so let's go ahead and do that first go ahead create an account on mongodb Atlas it's a free account on the official mongodb I'll be using the exact same for this entire one and you can just click on the database and then you can just have the connection string with that uh make sure if you're watching it for the very first time or you have uh loely some idea uh make sure if you go ahead and check out this portion of this uh when you're generating the connection string uh you have provided the network access to all or at least your local host and you have your proper username and password being selected in the database access these are the most important part of it uh once you are done with this uh then uh you can just click on the connect and get the string so if you click on the the connect you'll get the compass or you can just get the mongod DB vs code string anything anything will work uh that's the prerequisite of it now let's go ahead and click on browse collection and this is where you see all these collections so let's create a fresh database for this particular application I'll call this one as simply agree so probably like this agree and uh this is my database name and collection name what you would like to call this collection let's call this one as users feel free to call them students users whatever you like to do that I'll just go ahead and create that so this is my first one okay uh we'll have couple of more of these ones as well uh don't you worry I'll walk you through with that as well but right now this is the situation what we have now go on to your vs code if you don't have vs code go ahead and install it it's a pretty fun uh editor uh so in this you can actually go ahead and have an extension which is known as mongod DB for vs code once you install this it actually gives you this mongod DB here you can just click on this uh icon any time and then just say click on connect once you click on the connect it will ask you to provide your uh string which you got from mongodb Atlas mongodb plus SRV with your username password don't forget to change your password here because the string doesn't give you default password once you connect this hit enter and then you'll see the connection here on the left hand side it says admin local video but you can just go ahead and refresh this and you'll see all the new databases that you have created so in my case I have this agree dat datase which is having users which is what I want I'll walk you through how to actually work more with this in a minute but after that go ahead and we need to get some data and upload data into this one so that we can learn the aggregation Pipeline and perform some operations on that for this uh you have to go on to gist guest whatever you call that uhg up.com that's my account and look for something which says author. Json yes there might be a couple of others as well background section and whatnot uh this is the one which you are looking up for author. Json it has three major database sets which will be useful for us uh authors. Json books. Json and data. Json feel free to call this data. Json as anything uh it could be users it could be registered users students whatever you like to go with that uh we will learn something from the authors and books as well so let's go ahead and create those database and fulfill all this with that uh so remember we have the two more authors and books these are also important for us so we'll just go with that uh let's go ahead and create another collection which is authors and we need books as well uh let's go ahead and create that and we need one more which will be books Let's go ahead and do that Books Okay uh I think most of our job is all done from the mongod DB Atlas side but we'll come back we'll come back definitely first of all let's handle the majority part uh which is this one uh data RJ and just uh open this into raw format onto a new tab and and this might take a couple of seconds because it's a long really a long Json data list might take some time but not that much probably my internet is at a bad condition let me go ahead and reload that is it working looks like let's in the meantime open this one up oh this one is fast so let's first handle this so what you have to do is copy all the data that you have just copy this this one is the data for authors. Json so go onto your mongodb back again open up your agree and this one is for authors. Json so let's go ahead and right click on the authors and say that we want to insert a document into this one this will open up this small code for you which will say that hey I want to use the agregation database uh get collection authors and insert one we don't want to insert one we actually want to insert many at the same time so go ahead and use insert many once you are insert many you don't want to just put one object there so go ahead and select that and remove that and just paste this this entire array that we have copied and pasted just go ahead and paste that once you have pasted you'll find this play button at the top go ahead and play that and yes we want to save and play that this is going to just push all of the records onto your Atlas database so I I can just verify that if I go ahead and click on authors uh now the data should be fetched up so these are just dummy datas of course uh so what we have uh similarly we can add more data so this is our authors uh oh this one is all here so we'll just copy this this one takes time A little bit uh this is all of our users so now we can actually go ahead and close this we don't need you we don't need you uh we don't want to save it either let's go ahead and right click on users click on insert document the same process this is not insert one this is insert many we don't want to insert just one object we want to insert the entire array a really long one this is really really long uh just click on the play and yes and this will take a couple of seconds uh but it's usually fast now we have a whole lot of data in our databases uh so let's go ahead and click on users and hopefully we'll find a long list of users uh Yes 120 of many uh there are around 600 700 users in this database uh but these are required uh we'll just study them as well in a minute but one more thing just one more final thing which is books. Json since we are here let's go go ahead and do that one as well this will actually uh complete our preparation phase okay let's go back uh we don't need you we probably don't need you as well uh don't save last one books right click and insert document I told you it's the extension is like super super fun to work with and it's super easy uh many let's just go ahead and remove this absolutely easy and paste that all right let's go ahead and hit play yes we simply want to send it hopefully now things should be all good and in our database collection we should have books all right so books and authors are closely interconnected to each other the user is in itself a standalone database that we have so we'll be working now first of all before you go ahead I want you to walk through with this database you don't need to worry too much about it just want to give you a brief idea of how you can actually generate on your own as well it has index that's okay let me just zoom this a little bit uh we have a name we have is active parameter with a Boolean value we have some register date we have age as well gender eye color some favorite fruits then we have company as well so some data in the company we have location we have a tags and we have some array of the tags uh enm ID weth whatever that is just fictitious data picked up from the internet and similarly rest of them are exactly same so assume that you have built a startup where this is your company's data where you can have some of the users being active some of the being registered on XYZ date some having genders age eye colors some of them have tags so you want to find out information uh using the aggregation pipeline maybe you are building a dashboard maybe you're are building an admin panel this is where the aggregation pipeline shines and I'll walk you through with all of this how this actually goes so now let's go ahead and go into this database section we'll be working through majorly within the browser itself uh what you'll notice in the browser that all these users and all these things when once you select them uh you can actually click on the aggregation and this actually takes you to aggregation pipeline the code writing portion of it is exactly same as you can see in here as well that wherever you're writing whatever the programming language you're using this is how we write that the whole important part here is is this array because it has a lot of objects which are pipelines these are called as pipelines by the way and we will learn what to write inside this array that's the goal that's the whole idea and the the whole goal of this entire long video uh so I think this part is all clear to all of you that we have done the setup at this point our setup stage is all done uh now let's go ahead and answer these questions one by one so I can just go ahead and close this I don't need that don't need that don't need that don't need that either uh I'll be using uh my favorite website which is raser one of the very great uh descriptive website and from here we will be writing some of these questions and one by one we will be answering uh these questions as well of course by using these aggregation pipeline we'll study some of the docs as well but majorly we'll be going through with this one as well all right so once you have clicked on this aggregation pipeline you'll see that there are stages and there are text as well now stages definitely this is how the stages look like this is the first stage of the document then the you can add more stage of this document uh just like I walked you through in the documentation that hey this is the stage one then this is the stage two so you can do that from within the mongodb Atlas or whatever the client you're using for mongodb but what I want is this tutorial to be more friendly for programming perspective yes you can export this to language as well and they do support a lot uh from python to node to whatever you want you can do that but what I want to focus is more on the text side of it so that we can actually go ahead and write all these on our own and see that what's happening how it is happening and as you can see this is where the array is and this array can you can just press and enter uh this is our first stage of the pipeline then similarly we can have second stage of pipeline uh the for the second stage all the data that has been filtered in the first stage will act as like this is my source of Truth this is my data not the entire data only the data which is in the first stage will act as a source for the second one this is how we'll be doing that so let's go ahead and work through with one by one all these questions and we'll be going through with that okay so uh I have a lot of questions for you uh let's go ahead and answer them one by one so first one being what's the first question that we have throw up the first question how many users are active that's an interesting easy one but interesting one that how to find that how many users are active in this database so how would you do that uh probably exactly same that you'll go into uh aggregation pipeline you'll say db. users. agregate you'll use this query but the question is how can I build an API which answers this exact question that how many users are active so from the database you can already figure it out that hey there was a field which says is active false and there could be a field obviously there is a field which says is active true so how can I find out all the fields which mention is active true hi that's easy one so let's go ahead and try to write the query for that so aggregation pipeline doesn't work magically out of the box they use some of the operators of mongodb and hence I said that you will feel so much of comfort uh with the with the operators as you go through more with this one let me walk you through so this is my first Pipeline and in this one I use a mongodb operator known as match because I want to match a particular field uh what is the query that you want to match for and we write mongodb queries like this not with the SQL syntax statement and that's probably one of the reason why a lot of people love this so I know there's a field known as is uh active Okay doesn't really suggest me okay anyway I know that this field is available which is is active and I want to provide the value of this field as well this which is true so this is what I want to select okay now once you write this automatically you'll see that a sample of 10 documents is being selected but what's interesting is in this sample document I have all the values being selected as true you can verify that by the way you can go ahead and say Hey I want to use a false statement and automatically the pipeline executes that's that's the great thing about mongodb and especially atas portal now all the values that are selected are false but I wanted to connect only which are true okay easy step so first stage is where I've selected all the users but I want to give them a count as well so how can you count them you can pass all these users because they will act as a kind of a original data for the next stage so how can I write the next stage told you this object is the first stage let's put up a comma this is my second stage how easy that is uh I love this for this we have to learn another uh operator of mongodb which is again super easy you can just go ahead and use a count operator so what do you want to call this right now it says string so notice here it will count all the document which was mentioned in the previous stage but it says as string 516 I don't want to call it as a string you can nobody's stopping you but I want to call it something else probably as active user or users now it will say uh once you'll export that the output of this entire aggregation pipeline which is two stage aggregation pipeline is active users 516 how great that is this field never existed you never calculated this field but using the aggregation pipeline you can do this and by the way you can actually move on to the stages as well this exact same thing and you get the exact same result this is also doable where you actually in the drop down you select the values and all of that but I think writing the text gives you more Advantage as a programmer to understand what's really happening and how is it happening so that that's a great start so this is our part one of how we got started and of course we have 20 more questions to answer and these 20 questions will help you to understand properly that where does the aggregation pipeline shines what to work with that how to be cautious about it so that's all what we'll be doing in this entire video uh let's go ahead and move on to the next question all right welcome again to the mongodb aggregation Pipeline and this in this portion of the video we're going to only answer two questions of course many rest will come but eventually uh this portion will only talk about the two major questions that we will be able to get answer of them uh what are these two ones uh very interesting one let me walk you through with that uh so let me share the screen one more time again and the second question technically this is second uh is is this one what is the average age of all users H why is this so much interesting because this is a really difficult question to answer at a first place especially when we have a database like this uh if you just go into the database like this you'll see that uh we have these users I don't know how many of them have this age of 20 some might be having the age of 21 some might be having the age of 30 uh 31 I don't know there's a lot of numbers and at least from 1 to 100 if we look from the real real perspective that these many users could be there so there could be a group of hundreds yes literally hundreds and in each group there could be five people three people finding the average is not an easy it's an intensive task now that you look from at a boiled down perspective so we need to learn that how we can group them together because in order to calculate the average it's not really that much of a complex deal you just need to find out how many people are having the age of 30 how many people are having the age of 40 once you have all these groups okay uh this is the 30 age these many people this is the age of 40 these many peoples and then I can just sum them up and do the regular average operation uh which is not that difficult but right now the challenge in front of me the biggest one is how can I group the people together and once I know how to group the people together uh then I want to group everybody based on the age so that I can find out all the people and all these in the group so it it's a challenging task let's break it down one by one first can we actually find and group All The People based on the gender because I think that will help us a lot of understanding in the perspective of understanding let's go ahead and try that so once you actually use this kind of a situation uh the easiest way to be into this pipeline is let me Zoom this is the one thing is sure that I want to use this group yes that's my staging I want to group The People together but let's just say for the first perspective I want to group The People based on gender so how can I do that that's that's my first step if I select this it gives me all these things like what is this ID operation field accumulator can be confusing so we'll just remove this whenever there's a confusion remove that so it says you can give a ID with expression right now we don't have any expression but let me break it down you can use a string and then provide any field here with the dollar sign for example I can just provide that I I want to group People based on gender so once I do this you will notice that I was able to group all the people how many were there I was not able to count that that will come later on uh but I was able to group my document based on the gender so female and male H that's an interesting perspective now what can I do is uh can I actually group all of them based on age similar to this yes I can once I write this age here notice what happens I have people in the group of body notice there was nobody in the group of 22 probably 22 is there probably seven there was nobody seven in the database so I now have a group of 30 uh people 37 22 uh so yes this is working good at least I was able to group them together based on age but now I want to group everybody but not based on any field I just want to group them all together uh so that I have one big document itself because once I have this big document uh then I think I can calculate the average because it will act as whole one document so that's that's the role I want now in this case what you can do is you can provide here nothing and you can just go ahead and say I want to group them by uh nothing so I'll just go ahead and say null once you actually do this now this is a document it doesn't look like notice here it's just a one document so all the people everybody are in this document now all I have to do is now I can calculate the average based on just one field which is average so that is the power that mongod DB gives you a little bit hard to digest that yes you can pass a null here uh but this is what you can do now once I have this null now I can provide my accumulators onto this one if you remember when we saw the suggestion it gave us the accumulator so now I can go ahead and say that I want to generate a new field uh just like underscore ID is uh autogenerated field you can generate new Fields based on whatever you want to call them I'll call this one as uh let's just say average age feel free to name it whatever I like the bigger question is how does this field is going to be calculated that's where you define this so I want to calculate a field based on the average so I can use a mongodb operator known as AVG this is an accumulator but based on what this average should be calculated this should be calculated based on age now what you'll notice is once I write this I actually get an average age of 29 so that's my data Bas age but the most important part here that was learnable was I can provide an ID of null now notice here if I go ahead and say no I don't want to find the ID null I want to I want to group people together based on some field uh for example maybe I want to group them together based on uh gender so once I go ahead and say the gender now notice here I have uh based on every document was based and grouped together based on females and males because these are two categories two groups that can be made out of the gender so this is the average here this is the average here so both of them are being calculated this is just a little surprising for a result but yes you can go ahead and do that but once you go ahead and say that hey I don't want to give you anything just group all the document together then you can actually go ahead and provide the null of course not in a string because it's a keyword itself uh then what it does it actually makes instead of making different documents it just makes whole thing as a one document now since I have just one document I know how to calculate the average I know how to find this because everything is now summed up now this will get much more clear to you I know it's a little bit hard to digest as a fact because this is not easy this is not easy uh another question that we are going to answer now will help you to understand that how we were able to calculate this and then we'll come back onto this one uh the next question is little bit much more trickier than this but it will help us to clear the concept of this grouping as well uh let me go back and get this next question that we have and yes uh that's why I say uh finding and learning about the mongodb aggregation pipeline is not the easiest of all notice here this question also the next question also talks a little bit about average a little bit list the top five common fruit among the users so one thing is common or one thing I can deduce already that in order to find out the five common fruit or most common fruit I have to group them together because if I check out my database uh in the stages or in this uh there is something known as favorite fruit in all of this this is banana uh this is favorite fruit is Apple so first and foremost I need to group them together based on how many unique values of this particular field can be apple bananas oranges whatever that is I need to group them together that's my first step so if I know and understand this grouping at least half of my job can be done so let's go ahead and try this okay again let's do it from the Fresh uh scratch so that we learn about this one more time so first of all I know that my first stage and remember by the way if we are using aggregation we are learning operators at the same time you don't always have to have multiple stages of these things this is how the mongod DB works okay first of all what do you want to do I want to group them together okay so you just use the group operator uh but I'm not interested in these IDs and all these fields so I'll just remove them up okay based on what expression you want to group The together how you want to create your new documents that's easy I want all these uh favorite fruit that are here so I can use a dollar sign favorite fruit hopefully that's how we are actually depicting that in the database as well once I do this uh I have a new ID and I forgot the dollar sign that's why it's yelling and not working as expected so once I actually do this it says a favorite fruit I probably need to go into the stages copy this because I think I made a mistake I'll copy this and I go back here and I'll just paste this with of course a dollar sign okay now I have favorite fruit but now notice here how the document which has thousands of documents probably 500 600 I was able to group them based on their unique values this is what exactly it does if I go ahead and say that it converts all these 500 700 800 whatever the documents is into just three documents each one gets the ID automatically based on what the unique value is so notice here bananas apple and strawberry these are the only three fruits uh that we have as of now but this is just a sample this is just a sample of three documents there are definitely more in this one you can check them in the stages as well that hey this is how it goes uh we have banana strawberry apple right now we have three but there could be more there could be more don't rely on this sample because this is just a sample not the actual one okay so one thing is clear now I can understand how the grouping actually works if I go ahead give them a field it groups the values based on the unique values of that group if I give null it just makes a big long document and helps me to calculate uh the average on the whole document itself or any operation on the whole document whole database it does uh but here now now we have three uh particular subdocuments of this document okay so one of the thing which I have is I have grouped them together and now I want to count yeah that's that's the new challenge I have this IDE but I don't know how many of these bananas are there like how many people have this banana as their favorite fruit I don't have them I have just grouped them together but I don't have further information so I need to actually calculate this information uh what you can do just like we were able to calculate the average you can actually go ahead and create another field like we created average you can count them as well now this dollar count is actually the operation uh this count is my name for this field okay so count how do you want to count them you use curly braces again and then I can go ahead and say I want to sum them together you can actually use a dollar sum and with this sum you can provide a one here now this one is very interesting here this one simply says that the you found any user in the banana category let's say just add one value so this one is saying uh it's not a true and false it just add count one to this so let's just say it goes through the document it says hey uh this John is loving the banana so add one to this count uh this Jane it also loves banana so previously it was one now it it goes to two so that's how we are doing so once I go this now I have this count field and I can see more information about this document so wherever we have grouped all the users together uh the counting of this is 339 here we have 338 here we have 323 so okay now I know that uh there are 339 people in this one document uh which loves banana there are 3 38 people which loves apple and goes like this okay so this is what we have so far but this is not good enough because our question is a little bit more tricky list the top five common favorite fruit among the user so I was able to uh categorize my documents based on this banana I have uh this count as well okay good what more I can do I need to do sorting yes that's the first thing because I want to find the top five uh right now these are just in any order but I want to find and do the Sorting now in order to do the Sorting I have to pass this selection because this is the selection is being done now I can use another stage to do the Sorting that's a very common uh practice that is being done so let's go ahead and do that another stage this is how you open the stage and in this I want to use the Sorting operation very very simple just go ahead and do a sort uh by the way you can do sort just like this you have to provide based on what field you want to do setting and here's here is the another thing this count field is not available in my original database but since I am into the aggregation staging of pipeline now for this particular field or this particular pipeline the truth is this it the truth is not the original database so for this database the count field does exist yeah that's simple now I can just provide a field based on which you want to do the counting so here's my field I'm just going to remove this one I'll say hey my field is Count there we go I've given this field as count okay how do you want to do the counting I want to do the counting in ascending and that's where you do a minus one so now the counting is done and probably I did messed up uh probably okay no anyways it's it's all good if I want to do ascending order I can just give one if I do minus one that means uh the highest value will be at the top so that's how it works okay uh notice here this one says 339 338 and 323 what happens when I do just one uh really simple it just changed that now it's 323 3 38 and 339 so you get the idea this is really simple it's just really nice okay now I was able to group them I was able to count them I was able to sort them but I only want uh three out of this whole thing like I want to find out top five top three top one so I can provide one more pipeline here and I can just go like this so I'll just go ahead and work on with this another pipeline is being added and in this pipeline all you have to do is I want to use another uh operator which is known as limit and you can just provide a number I want maybe just two so you can just limit all the values uh for the two so these are my top twos bananas is favorite for everyone then comes the Apple maybe you want to find out top five so go ahead and provide a five that is how it works don't worry I'll repeat this one more time because it takes a little bit time to absorb how and what all these things are going on uh this grouping not at easy not at all easy to digest it takes some time it took me some time as well so don't worry I'll rewalk you through all these things along with the first question that we did and then the second question uh that we did so notice here okay now let's go ahead and try to get back onto the first question so what is now the average age of all user now the challenge between finding the average age of all user is I have many uh subdocuments or documents whatever you want to call them uh it's all scattered right now so if I go ahead and remove everything from here and I just check out what all the users are uh notice here how many documents we have there is so many things uh this first user this second user so there is no way of counting or finding I just need one so that I can apply these operations I need to group them together so that is why the aggregation pipeline comes out so whenever you want to perform any operation to all the users uh we need to actually group them together based on something and sometimes you don't have anything so that's why this comes so let's go ahead and answer this one more time and I'll walk you through hopefully this time it will be little bit easy for you to understand this what is the average age for all the users so I know this's couple of things now from experience that okay uh there is an average operator I can use and since I want to perform something with all the users of my database I have to group them together but I don't want to group them based on favorite fruit or something so I have to provide a null as an ID so this is how you build knowledge on top of knowledge so now since you have seen that in the past if any similar operation comes to your scenario you can use this knowledge so it would be super easy for you you can go ahead and say okay I will use an aggregation pipeline uh so I'll just use this one first I want to group them together okay so the first thing is because without grouping you cannot perform any operation to all the database users or all the database documents so first field is let's just say group I want to group them together uh I don't know what all these are so I'll just go ahead and remove this okay so I don't know what you are talking based on what field you want to group them all together I want to perform this operation to all people I I have no criteria right now so just go ahead and provide a null okay now I'll know that I don't have hundreds of document I have just one I have now just one now I want to find that the average of this one so how can I find the average of this one I can create my own field now I can call this one as count I can call this one as average whatever you like it I'll probably use average age and how can I find the average age I don't have to do the manual work of it mongodb will do the manual work for me and I'll just say that hey you know what this is how you calculate the average age for this one you will use the operator which is average which is an accumulator Operator by the way if you don't know what accumulators are uh go ahead and revise the basics of JavaScript uh just like reduce is an accumulator uh average is also an accumulator for mongodb so average and how do you want to calculate the average uh the way how you calculate that is really simple I want to calculate average for uh age so I'll just go ahead and say Ag and that's it the average is being calculated for that uh really really nice and now we find the average age so anytime you feel any situation where you want to calculate the average of maybe pricing maybe age or something exactly same knowledge could be repeated I hope now it is making much more sense and you're taking the notes as well I told you uh let a little bit difficult to understand but hey watch these videos or this section couple of times now coming on to the next question uh list the top five most common favorite fruit among the user okay so I know that in my database uh I have a favorite fruit as a field so if I go ahead and look for the stages or somewhere here I know that there is a field known as uh favorite fruit yeah here it is favorite fruit I know I'll copy this I know this is a field there so I need to group the document because I want to perform some operation I want to group all the document based on this field how can I do this okay let's start with that the step one is grouping so let's go ahead and say hey uh let's go ahead and use the grouping based on what do you want to do the grouping and remember always delete these accumulator they might confuse you at initial stage so remove that what is my expression based on what particular field which is present in all the document I want to group them together if you don't want to give any just use null but if you you want to give any just use the dollar sign and the name of the field now based on this uh this is the unique ID for that a unique documents are being created and we have now the grouping of them okay now once you have the grouping then maybe based on this group you want to do some more accumulator actions or something more I have simply one I want to find out the counting of them not the average although mongodb can do that for me as well I can find that uh but this time it makes sense to have the counting of them so create your own field now let's just call this one as count now how this count field value will come in you have to tell that mongod DB the counting will come up from another mongod DB operator known as count so there we go count not the dollar count just like that I have to use oh by the way sorry I can go ahead and say I want to use the sum of field in this one not the count literally so sum them all together this is how the counting will come notice here almost every time uh you you're using performing groups or something you'll use some kind of accumulators accumulator simply means that it doesn't calculate the value fresh it just accumulates it and counts on top of the previous value that's what the accumulators are so now we have the sum and the way how you use the sum is also a little interesting you just use the sum like this and then you have to say that how many increments you want every time you find that particular value I want just one increment but uh there's nothing which is stopping you to say that I I want to count two every single time I find the value obviously you'll find the twice of result but hey Nothing is Stopping You to do so so now I have another field which is count and I have three or how many documents you have all these bananas and everything all right now now now we have grouped all together and we have this new field we can actually pass this whole thing into another stage the staging the pipeline uh which can does sorting for us so let's go ahead and put up a comma and let's do the Sorting I'll use it like this and I'll say hey let's just sort this so there's an operator for this now that you know about it uh this is a sorting what field you want to sort I have a field I know about it that's my count what is the Sorting order it could be one or it could be negative 1 based on you want the highest value on the top or the lowest value on the top negative 1 means highest value on the top so we have highest value on the top okay uh so the Sorting is done but you don't want all the results maybe the front end guy is not looking for all the results you want only the one result uh pass on this pipeline to another guy so let's go ahead and pass the pop and say that hey this one is going to be limited to only particular document maybe you want to send only two so let's just say limit and the number is two I only want to send you the two values of the Top Value so here we go super easy super simple now with this this particular section I would highly highly recommend to study more about the grouping so if you go into the mongod DB there is so much more to study but go ahead and click on the search and just say that hey I want to study more about the group so just hit enter and this is the point where you actually study a little bit on your own uh not get into the tutorial hell or something this is the aggregation which we want to study and notice here what it says little bit let's study together the group stage separates document oops sorry sorry uh the group stage separates the document into group according to a group key the output is one document for each unique group key that's what we did in the case of bananas and apple but what we studied and learned on top of that that we can provide underscore ID as null and then it makes the entire document as one so you might be wondering can I go ahead and just perform this average directly uh yes but actually this is much better of a way and probably we'll discuss more a little bit on on later on stage uh but notice here this is how you create the field learn a little bit more accumulator add to set average bottom so there's there's lot more field you can uh learn about it return an average of numerical values ignores the non-numerical value that's why we were able to do the average of the ages we but we have bottom count first last Max median and again I cannot just go ahead and show you example for each one of them I've tried but something like you want to calculate the percentile now you can do this now you can calculate the percentile for all the users their age uh maybe their marks minimum marks uh you can find out the people who have the five people who have the lowest marks something like that so go ahead study a little bit more about these uh documentation more onto that side uh that's all for this one very similar uh in this particular section we studied just uh the two questions but in the later stage we'll we'll definitely get more onto this one all right so I hope you took some time pause the video there if you're watching it in one go uh this whole thing of grouping the data takes a little bit time to actually adjust to your mind digest the facts and all of that uh but now from this point onwards you'll be actually grouping them much more easier even if you have not read too much of the docs but you'll find that okay I know how to dogs and how to deal with that the next three question that we'll do in this segment or this video however you're watching uh will be much more comfortable to you surprisingly yeah that's that's the fact uh okay so let me go ahead and walk you through let me share the screen first so this is where we are and and what we're going to do here is first of all let's remove all of this we don't want to have this uh now we have nothing inside our this guy okay what is the next question that we have in front of us in fact we have a lot of questions going on in here the next question that we have is find the total number of males and females very interesting question that we have and I think you might have already figured out okay this has something to do with the grouping otherwise that's not possible yes you are absolutely correct that is only possible by grouping them I know that if I provide underscore ID to null it just groups everything as one but if I provide a particular field then it groups them based on the field H you are absolutely correct I know based on my past experience that I can do average I can do sum uh so if I try sum after the grouping can I find uh the number of males and females in this entire document probably probably yes let's try this out uh we don't know the exact answer how to do that but we know some things that can be done for this one so let's at least try that one so the first thing that I want to do is obviously that group okay uh group I don't know what these accumulators and all these things are I'll still remove that and I know the expression that I just want to group because I want to find out uh how many of them are males and females field is gender I know that okay let's go ahead and use the expression and say dollar gender uh if I do this this now I'm pretty sure that it actually grouped all of my documents in just two because there are two unique values based on the field that I'm providing here so this is gender okay now previously I saw that when I was doing the average not a great idea but can I at least count how many document you summed up in the female uh document and in the male document probably yes I can generate a new field based on that so okay let's try let's count this or gender count I think that would be a better name gender count uh I don't know running out of the names here but how this field is going to be calculated you put up a curly braces just like that so in this one now I want to find out how many each values are there so you you guessed it that will be a count okay uh let's go ahead and use a count for this one yes that's the accumulator I want to use okay now I'm using this count but the count is not going to be counting like that I have to use the same strategy that I used in the past that is uh simply saying I want to count based on the sum so the count actually works doesn't work like that so I'll just remove this one and instead of counting I'll use the sum yep that's the accumulator I want to use not the count I want to use the sum how do you want to use the sum every time you find a unique value just add one to it that is how I want to use it let's see what happened I say the gender count so for the male it is 493 and for uh the female it's 507 so again uh maybe the gender count is not a good name you just want to call it as count totally up to you totally on to you uh sometimes you'll find that what more most people do is they just call it as count this count is not your accumulator count that is a dollar count that is a different one I just want to use a account uh but what happens when I go ahead and use it something like this all of the this doesn't really bother but if I go ahead and use the count this is how the accumulator works and then you can provide that hey I want to count based on any field for example I want to count based on gender uh doesn't work it's not going to give you the exact result that I want but just wanted to show you that hey this also works here but not an accurate depiction of what you really want to do uh some things work something doesn't work but we don't want to fall into the category where we just calculate anything so anyways what you really want to do in this one our question was really simple find all the male find all the female I know I want to group them together based on the genders very clear but what after that how can I count them counting is done through the operation of sum not the count I want to sum them because it's being grouped all together that is why I use the sum here and say that hey how want to sum that every time you find the unique value in the document you count that just add one to the sum and that's being added so this is much better much better off a result now the next question question that we have that we have to get the answer for this we have to study our document a little bit but it's not really that much of a big deal uh the next question that we have is which country has the highest number of registered user H interesting let's see and figure out how our document is actually being made up of uh let's go into the stages and let's find out with any one of them I know uh that there is company object there's this object I need to actually zoom out like this and where we find a company so something related to companies there has the highest number of registered user country okay how can I find the country H that's already very very interesting I don't see the country but I do have few objects here company tags let's explore them okay in the company I have phone I have location which is further an object and I can open this up oh this has a country so this time instead of directly having like gender eye color age or something I need to drill down I have to drill down to find out so I can actually can I group them all the user based on this country tag because I know the this is how I can find the country okay I'll figure out a way how to do this but what's my next thing okay I've grouped everybody based on the country has the highest number of registered user okay so I have to first figure out counting the sum okay that's great B based on the country I can find the sum okay then I can do the Sorting again based on the previous knowledge and then I can find I can apply the limits yeah that's interesting the only thing which is bothering me right now is how can I drill down into this one if I know this most of my job is done okay let's do that let's go into the text field and let's try this again one more time so we are again into this part uh we want to group them okay how can I group them I can just use a group there we go I don't know what these extra things are so I'll just go ahead and remove all of this now expression how can I group them together okay let's remove this now first and foremost what you have to do is use a dollar sign and I know there is a company that's the easiest one but I have to further drill it down the best part is you can drill them down just like you do them in JavaScript so you can just put up a DOT and you can say location that was it and after that there is a country and that's it yeah literally that's it now you have grouped them based on the uh company drilling down to the location drilling down to the country okay now my first part is done now I have all these France USA Italy Germany all these countries sample of four documents but I want to count how many documents you subbed or you grouped based on France based on USA so the easiest way is to do the counting but no this is not counting this is summing up because that's the common mistake a lot of people does so I'll just go ahead and say Hey I want to create a new field let's call this one as user count country count country user count whatever you want to call them and the way how you're going to calculate is not the count you're going to use the sum okay how do you want to use the sum just add one every time you find that so now that we can see that from the USA we got 225 255 261 and we have 239 so the next step is to sort them and based on my previous knowledge I know how to sort them this document whole thing can act as a source I can just open up another Pipeline and I can say hey just use the Sorting so let's just go ahead and say uh sort how do you want to sort based on which field I have generated a new field call user count and what will be the sort order highest should be the top so negative one okay so you can see as you go progress more in the tutorial you find more Comfort if you just have stopped the tutorial and there might be some people in the comment section I couldn't understand it some things you need to stay with them and that's how it makes sense notice here now we have 261 uh 255 good I want to find only the top one country maybe top two country how can I do this based on your previous knowledge you know I can provide a limit there and I can use another pipeline so I can just go ahead and say hey this time let's use a limit and I want only top two or top five whatever is being asked to you do so so you can see we have Germany on the highest count hey go Germany and we got 261 and 251 so I hope now it is making sense to you that okay how can I group these things how can I group them okay uh we have one more question to do uh in this one I think now it's making more sense to you let's go ahead and another question okay find the total number of males and female I think we have already done that yeah so oh my bad let's bring up another question which I have for you list all the unique eye color present in the collection very very great question uh list all unique eye colors how can I find the eye colors let's first analyze the data that we have so in each one of them I can see there is a eye color parameter I'll copy this one so this is green but I'm sure there could be more eye color oh there's a blue there's a green and there could be more uh yes we have more uh what's the I color green so how many of them are there how can I find them out hm how many eye colors so I probably have to group them together and I think that can do most of my job and in fact that's the job group all the values together based on the eye color field and that's the answer for this one list all the unique eye colors Ah that's nice let's try this out I'll go into the text and I know that I have to do the grouping so let's go ahead and remove this and let me Zoom this okay what I want to do I want to find all the unique eye colors I have to group them okay let's go ahead and use a group I don't know all these things I'll just remove them based on what field you want to group them okay I have a dollar sign and based on eye color and that's it I have grouped them together now how many grouping I was able to do that's it the IDS are unique now if this response goes onto the front end it's easy because it will give us the array as a response return and each field will have a unique ID I think that's the best Advantage so I have found that there is only three one and by the way you can pass it on further to do the counting if you wish hey I want to do the counting you can just do the count as well uh if you want to do the Sorting you can do the Sorting as well but I don't think so there's any requirement the question simply says list all the unique colors once you group them grouping automatically works with only the unique values of a particular field that you provide so I think that that does the job so yes really really interesting and I told you once you start doing these questions they actually help you to understand tremendously and based on this tutorial you will build up the knowledge about the count about uh the sum about the average about the grouping that you can use this knowledge and apply them in whatever uh the aggregation you're doing in your uh in your work that you are doing or maybe the admin panel control panel datea analytics whatever you are building you can apply this exact knowledge uh that's how it builds up that's how the knowledge is built on top of that all right so that is all what we'll be doing in this one uh we'll catch up in the next video we'll explore couple of more questions slowly and I'm breaking this all up into smaller segments so that anytime you can pause the video do a little bit more research try to do all these operation with your hands see the result of them uh gives you a little bit more time so that's it for this one let's catch up in the next one how are you folks I'm pretty sure you're doing great and in this section of the mongodb aggregation pipeline we'll be answering just one single question yes it's a little bit shorter one but this will build up a lot on the knowledge and based on this knowledge you can build a lot on top of that let me show you why this is so much important so here's the screen and the question in front of us is this one what is the average number of tags per user this is the only question we need to find the answer so what's so much important about the average we have seen the average we now have a knowledge strong Foundation that we can group anything together we also know the fund fundamental and Foundation that when we say ID is null it groups the whole document as together and we can perform averages and sums and whatnot onto that but what's a special about founding the average number of tags per user that's where the things are interesting let me show you if you go ahead and look onto our database you'll find that if you just take a look on this find section uh you'll see that we have a lot of things going on with this one this first user that we have is having the array of five and I cannot scroll when I'm zoomed in in too much so I have to go out okay there we go I hope it's visible let me Zoom this a little bit more okay hope it's visible now so here we can see the array is five by the way the video is entirely in 4k so you can just click on the settings button and get it more uh visible so here we can see the array is five and if I scroll there we go array with four elements and here we can see array with four elements then three elements so it is quite important that we find out the average of the tab and that's what the question says what is the average number of tags per user so one thing is clear I will use the group activity but I need to somehow group people a little bit smartly than I learned in the previous ones and if I open this one up uh this is just an array with the indexing and there are a couple of ways how you can actually preserve the indexing and whatnot we are not going to discuss that in this one but so the question comes is how can I write the aggregation pipeline for the same in fact there are a couple of ways how you can do this exact same same situation and there are a couple of things which should really come to your mind let me just zoom this there we go so the first thing that should come to your mind while writing an aggregation pipeline when you have to deal up with the arrays is we need to separate out these array without separating out these array you cannot find anything inside the AR and there is a special method of doing so although this exact problem can be solved with couple of ways in fact I'll show you two but the first thing anytime you see an array and you want to kind of rip apart those array spread them around then the first thing that should come to your mind is unwind that's the operator we are looking up for so the operator is known as unwind the whole idea behind this unwind operator is that it just I'll show you that I'll show you that's much more easier to visualize so imagine that there is one user and in the array there are three elements so what this unwind does it creates three document for that user how why three uh because the user is having three elements in the array so now it will create a new property in which uh the first tag is there first index value of the array then the second user will have second value third user will have third value the ID of the user will remain same but the only property that will change is the array value so if you have an element or a user which is having 10 values in the array uh same document will be uh repeated but same ID but 10 values based on the AR yeah so get that a little bit uh shake your head try to understand that unwind just rip apart those array create duplicate values same ID but different values in the array that's the important one now there are a couple of options that you can see here index include array indexing uh preserve null empty arrays and whatnot uh we're not going to go into this one as of now but feel free to read more about them that's not really that much all you have to do is let's remove this first and you have to say that hey how should I unwind give me the path where I can find the array So based on that I can rip apart the entire thing uh easy way uh just go ahead and use the strings and inside this dollar and I can use the tags that's it now this actually unwinds the thing so that is my first uh part of this one all right so unexpected token uh it shouldn't be comma there okay so now this should be all okay let me just check whether this is all okay by the way there should not be any path if you're passing just one parameter you don't need to even give the path uh you can just directly say that hey I want to unwind it based on the tags there we go uh should be all happy if I go ahead and add this one there we go now what you'll notice in this scenario is every single user you'll find a duplicate of that like for example if I go ahead and look this Oria Gonzalez uh you'll find that the tag now just says tags now says uh NM which is one the same user will be repeated again orela Gonzalez but this time the tag is different so here we don't have any tag yes here we have uh tags which is ID so ID is another tag by the way inside these tags and here you can see the tag where is the tag tag is valet so whatever the values are but you can see the user is exactly same the user is exactly same it's still Ora Gonzalez so same values being exploded around but with the different tag values the only difference is tag okay now we have done this now the values are spread around now what should I do next and what's my question the question is average number of tags per user now your old knowledge comes into the picture just group them together find the average and that's it that's all we got we have to do so now that this pipeline is all done what I can do is uh let's just put up a comma hit and enter and now let's write the next pipeline what do I want to do I want to group them together so let's use a a group if I can write that there we go now again uh we are not interested that's too zoomed in let me just let me just manage this a little bit probably this is too much zoomed in I'll just zoom out yeah this is much better for us okay again uh the same thing that we used to do exactly same so we're going to just remove this one uh how do you want to group them together now this time I want to group them based on their IDs so how do we do that let's go ahead and remove that and this time I want to use dollar ID classic popular way of adding them together with IDs because remember I told you everything is duplicated uh the only value that is new now is the are value the IDS of the user is still same so that's my first way that hey now I have grouped them together now I want to find the number of tags so what I'll do is I I'll introduce a new property here I'll call this one as number of tags so average user I want to find how many tags are there in you so I'm going to just go ahead and say how to calculate the number of tags for this use an operator let's call this one as sum which is an accumulator again remember we discussed the accumulator and every time you find that increase the value by one so so now that we have grouped them together and now we have this average number of tags which is a new property that we have introduced while grouping them after unwinding them a lot a lot uh but this is one so this is my second pipeline that I'm doing here now what could be my third pipeline in this one I want to find again the question Still Remains uh how what is the average number of tags per user so how to find the average of this one now it's easy now you can do another simple pipeline by grouping it and averaging how going to group them you get it right we need to group them in all in one document we do that by ID null yes exactly so let's go ahead and do that again same thing we'll say hey I want to use a group but this time I'm going to zoom out a little scroll it Zoom there and we'll remove this one whole thing hey how do you want to group them I'll just say hey null that means now it's all being grouped now we want to find the average so I'll introduce a new property I'll we call this one as average number of tags number of tags and how can I find the average here that should be super simple to you by now use a method operator we can say average and how does the average does the accumulation based on what field so I'm going to go ahead and say hey you know what we are going to base this on a new field that we added number of tags I'll copy this and I'll paste this and this will give me the average number of tags and if I didn't mess it up uh the comma is there oh probably I'm missing one parenthesis yep probably yes okay so now we can see that the average number of tags in this one is 3.56 and doesn't make sense we have to obviously uh cast this into integer uh but you can see that we have learned a new skill now that how we can use unwind so anytime you mess up with an array uh the first thing that should come to your mind is how can I use unwind for my advantage in this one but there is little bit more that you can go ahead and just work with that let me show you that part as well I hope you have taken a screenshot or something of this one uh this is how we are going through with it now let me show you another way hope you have taken the screenshot of all of this uh by the way let me Zoom this okay this is the best we can do this is the best this is the best we can do okay uh let's go ahead and remove this this exact same question could have been done uh with more knowledge of the operator let me show you that what you can do is there is also an aggregation pipeline which known as ADD fields which adds a new field into the existing documents and you can take advantage of this one in this case but I really wanted to teach unwind that's why I use this one so add wield so what is the new field that you want to add and I'm going to call this one as number of TXS this field doesn't exist but I'll just go ahead and add this one so how this field is going to be calculated that's the interesting part so we're going to just remove all of this we don't need this we'll just start that how this field is going to be calculated for this one what you can do is you can just go ahead and use a size which actually tells you the size of an array and yes uh this is exactly what why we did the unwind we did the unwind so that we can find later on can group them together based on that so now you can use this directly size and all you have to do is provide that hey how the size needs to be calculated so in this case I can just go ahead and say tags and it will calculate other tags uh the field of the tags here but the thing is now we have number of tags five in this one for this one but this is mongod DB that means there could be a chance that this tag might not be present in all the documents what about that case you have it handle that case so in that case what you have to do is uh just remove this portion of here and handle that situation so what I'll do is in this size uh there is another operator known as if null so if you found this particular field null what to do in that case so if null uh then uh use the square brackets in this one and then first of all the first parameter is where should I look for the the property which is in this case tags but what if I found this property as null what should I do in that case treat it as an empty array that's it a little bit difficult to understand and graas it first but you if you look at from The Logical perspective you'll find it much more easier so add Fields adds a new field in the document the number of tags is the name of my new field and I could have directly calculated the size we just saw it works but what about the case when this particular tag doesn't exist so if it is null then in that case we'll treat as an empty array so we don't have any example as of now I think in almost all of them they have the tags but you get the scenario okay now we have this new field known as number of tags uh so the tags are five so we get this property so now based on this how can I actually calculate the average same thing now we we have instead of unwinding and winding them again we have done that easier way so I can chain on another pipeline here so I'll just go ahead work like that what should I do group them find the average yeah that's it that's all we have to do so we're going to go ahead and say hey I'll like to group them how would like to group them I'll just remove all of you and I'm going to just go ahead and say null now it wraps up everything and I want to add one more property here which will say average number of tags really long one but how can I calculate the average for all of this simple just use average this is an accumulator So based on what field I should calculate the averages really simple just use a dollar sign and give this field number of tags copy that and paste that and there we go hopefully it does the same thing and yes we get the same results so we saw two ways of handling the situation exactly same situation the question was same there's no right and wrong in this one there's no performance or anything like that uh they exactly use almost same operations uh that was a three step pipeline this one is two-step pipeline uh but almost the same amount of work is done almost not exactly so in this one we just saw that how we can find the average number of tags and we learned how we can deal up with the array uh I hope you are finding this style of learning the aggregation pipeline much more intuitive as compared to hey this one is used for that and that we are actually going through the scenario based situation I'm enjoying this quite a lot hope you are as well let's go ahead and catch up in the next one and try to deal up with one more similar situation all right so welcome again to the mongodb aggregation pipeline my name is zesh and you know me by now so let's go ahead and get started in this section if you're watching it in a long video we will be answering three questions the first one you can build on your own as well and now I'll walk you through that how you can build similar kind of questions for yourself as well based on the data and be curious about it so let's go ahead and get started let me share the screen one more time I'll do that couple of more so there we go so this is our sample document that we have in front of us so let me just zoom in there and we can see we have tags a lot of company details objects we will study more about them objects are not really that much difficult to work on with because you can just access them with the dot notation easy part the arrays or the tags are a little bit difficult to find out so we want to answer some question s based on this so let's just say we want to answer uh how many users have a tag of enm or ID or valet like how I don't know how the team got this data but anyways uh so we want to be curious and want to find out that in one of the array uh we have a field of enm so how many more such users are there maybe I'm curious about ID or wellet or whatever that is so I'm just curious about that so let's first and foremost go ahead and write our question in front of us so I have one question so I'll just copy this and bring this question to you so that at least we have the list of the question by the way I'll uh give this entire workspace in the description I'll share this entire one uh from the very first video Don't You Worry so the next question is in front of us is how many users have mm as one of their tags so it could be any other tag as well by the way so we need to learn a little bit more of the aggregation Pipeline and some of that stages to work with this one because now it's not about grouping the data I want to have some filtration as well so how can I use filtration in the mongodb aggregation so what you can do is you can just look for something uh aggregator uh known as match just use a dollar sign before it because it gives the result on the very top for the aggregation so dollar match so this is the one that we are looking up for let's go ahead I wish there could be a dark mode somewhere hidden in this one as well thank goodness I found the dark mode on this one uh so this one is pretty easy it filters the document to pass only the document that matches the specified condition H that's interesting what's more interesting about this one is this particular match its query is actually much more powerful uh it can just slide through into uh the arrays the objects that that's what I love about this one so by the way you can actually learn more about it there is so much more that they gives you like author Dave you can just go ahead and find out the easiest one let me show you how this could be useful for in our case so we are here and we'll start our first pipeline just like this and we'll use the same match so what is the query that you want to match for so I want to match for a particular tag that I want to use so go ahead and write the query the query in this case it I want to be specific about a field I'm looking up for tags so I can just go ahead and use the tags just like that and what you're looking up for in these tags uh what I'm looking up for is now I can give the NM or feel free to give any other value as well so this is my my first match tag and now you'll see that it finds out the values uh but every single one of them will have this uh ID here uh you can just go ahead and give it a try again for this one so it will automatically go inside the tags and will find if it finds the NM it will filter that and now with this particular stage we have all the documents which match this criteria so the first part is filtered now we have a document set of all the users who have NM in one of their tags okay how many of them are actually there oh that's that should be the second stage and that's easy I just want to count all these documents and that's what the counting is for not the sum sum is to actually aggregate u in accumulate all of that but I just want to count all of them that's easy I can put up a comma start another aggregation Pipeline and I want to use an operator known as count which counts all these values by the way uh you can just go ahead and use a dollar's count and it gives you a count pipeline stage returns a count of number of documents at this stage of the agregation pipeline so at this point of stage I want to count all the documents are there that's exactly what I want so I'll just say hey what do you want to call it as you can definitely call it as a string but I don't think so that's a good name so I'll call this one as user with NM tag or whatever that tag is if you want to find out more of these tags like there is one tag known as ID you can just go ahead and modify this and obviously you'll get the different result so really nice we studied about how the match is another stage which actually filters out the document so consider this almost a JavaScript equivalent of filter but it's really nice okay uh we do have couple of other uh aggregation uh pipelines as well so let me bring up the next question in front of us which is kind of a repetition but sometimes reputations is good okay the next question that we have what are the names okay this is a little bit of a new one but I'm pretty sure this is going to teach us quite a lot okay what are the names Nam and age of users who are inactive and has vet as a tag okay uh we'll build on the existing knowledge so the first part or the last part is I want to find all the users who have valet as a tag okay I can definitely do that that's the easy part but there's little bit more to this what are the names and ages okay so it's not looking up for to give me the entire document it only wants couple of fields from that one uh names and age specifically of the users who are inactive and have valet and asag okay so this time I have to worry about two things at the same time uh the tag should have valet I can do that whilea the match we have studied about them but I'm also looking for their active status so we know that in our database or the set of document or set of database that we have we have a field known as is active and and we want it to be the false now if you'll study a little bit on the match further down the road uh you'll find that you can actually use a couple of of pairs uh for finding that so here the match they just gives you one authorized Dave uh I think there is there are a couple of examples here uh but you have to go through and look around but I can show you easier way of doing that okay let's start and based on this okay first problem I want to find all the users who are uh inactive uh uh is active as false by the way uh if you can see this we have is active as false so that's the one I'm looking up for but also I'm looking up for where the tags tags tags here it is where the tags is NM or wellet or whatever that is what was it by the way wellet okay so these are the questions we have to deal up with okay first stage of the pipeline obviously the stage one is easy I want to use a match okay what is the query that you want to go for now this time the first query that I'm looking up for is active and this is active should be false okay so it will give me all the inactive users okay cool but how can I I put the same query should I should I set up another stage and pass on this to another one you can that is also one of the approach but the best part is I can just put a comma here and write an and query yeah that that's the one so I can just say I want to use tags and one of the tag should be wellet or whatever that is how cool that is now it will match both the criteria but the problem is uh that you know what this is active false and the tags which are looking up for wellet is giving me a problem here H why is it giving me the problem so we need to study a tiny bit more that how we can match up and pass on uh this document all at once all right just looked it in uh probably there was some typo or something or probably something wrong with the mongod DB not pretty sure uh but what I did is I rewrote the query and it actually worked because the idea the concept is exactly same but sometimes typos does happen anyways let's do this one more time and uh hopefully this time it will work right out the box but I'll show you one of the scenario how I avoid these kinds of typers so anyways one more time I have this exact same thing we go with the match this is what we have now I want to write query the first query that I have is is active which is false that I want to go for and the second query that I want to go for is any of the tags so one of the tags that I want to use is valet so this is what we wrote and we can see now it is automatically popping me up all the values that I have uh somehow but I'll show you one of the easiest way of doing that actually this is shown in the stages format if you just switch on to the stages format this is where they write it nicely a little bit nicely than what I'm writing so they put up the match by the way all these pipelines are available in the drop down if you look for that uh there's so much of them I've tried to cover a lot but again it's not possible now here they write it much better and once you actually switch and try to switch it on the text now it actually aligns it properly uh you can by the way add more condition for example if if you are looking up for age 20 you can just go up here now and say age I'm looking up for 20 so that will also match that condition and we get the values up here so you get the idea how this is being done I'm not looking for age as of now so I'll just go ahead and remove this but you get the idea how this is being done now this reminds me that there are a couple of output options that you can use like for example if you click on export to language and you can just click on this and feel free to pick up your own language that how the exported pipeline needs to work in node uh you want to include the import statement you want to include the driver syntax so it us gives you pretty much everything which you want and that's what I love absolutely about the mongodb atlas but you get the job okay uh one part is done that now we have a match of all the people who are active and has the tags of valet uh so what was my question okay name and age of users who are inactive so is active is false that's great and have the tag of valet so I have actually aggregated this last part of it but now my front end is requesting or whoever is requesting from the API what are the names and age of this user hm if I go ahead and just give this as a query uh this gives me ID is active age index name now that that's not good I should be filtering it out the data yes I can do that while writing my back end but if you know how to properly write your aggregation pipeline you can do this All In pipelines itself for this we have to study a little bit more so go into the docs and this time we want to look for is Project this is really the nicest one of my favorite one project what it does it actually pass along the documents on the next stage with only the filtered or selected values and by the way they write it much better than what I'm explaining uh passes along the document with the requested field to the next stage in the pipeline the specified field can be an existing field from an input or newly computed value what this is saying is hey if you use this project then you can actually uh just filter out some of the fields from the document and pass them out now these fields could be in your document or you can just be these fields can be newly computed one just like we did uh counts and all of this so from the previous stage pipeline you can just grab them so this is what they are saying okay how can I use this again let's go on to the new uh pipeline there we go we just want to use this project there we go what the project does is what are the specification you want to go for so I'll just go ahead and say Hey I want to go with the project just remove this what are the all the fields you're looking up for just provide them for example I'll I just want name uh whatever the fields you want you just want to put a one after that I want the field name there uh what else you want what else they are asking for age Okay Age so the next one is age so you go ahead and say age one by the way this if this is bothering you uh just go back onto the stages they actually properly uh does all the Align and everything this looks much better than what I'm writing so there we go we can see that now it has done the job now I have filtered using the match and I've have also uh done the projection I don't call it as project I find it much more easier to call it as projection like what do you want to project of this data the projection is something which I control uh really really really nice I think that's a great knowledge that you have now okay uh now one more thing where you would love to take help a little bit from the chat GPT as well for the next question that we have all right next question what do we have okay how many users have phone numberers starting with uh + one94 okay uh this is really interesting but I still wanted to cover it first of all let me accept I'm not a master in Rex and especially these days I used to use a resource known as Rex r.com but these days I don't use it even even a tit name bit I always ask it for with the chat GPT or mostly with the GitHub co-pilot that hey give me the Rex for this one and and I did exactly same give me a Rex for this one but I'll show you how you can incorporate Rex in uh queries like this okay I want to find the users who are having this one so obviously I'll use a match dollar match as a pipeline and all I have to give is a reject into this one so that it matches the pattern in a particular field okay what field that might be uh let's go ahead and first remove all of this there we go okay let's check out at least one so we can see that uh the phone number Where is the phone number the phone number is inside the company if you explore this you'll find the phone number in the company object so it's pretty easy company. phone I can find that no big deal I want to match a rexr which matches the expression of plus one and 940 feel free to ask Chad GPT to generate it for you and yes I literally did ask Chad GPT to generate it and that's why I have it pre-built on my clipboard anyways let's start the pipeline should be super easy for you now uh it's dollar match what is is the query that you're looking up for I'm looking for a query in the field company. phone this is my field what is the regular expression you're looking up for I'm looking for this one uh this one says that everything that starts with plus one then a space then 94 Z then whatever that is so it matches all the values of that uh so it will give me all the values like this but I want to count how many of them so how can I count them yes in the next stage pipeline that's that's the one okay hope you Sav all of this in the comment section as well probably you were shouting on this on the seat no worries okay how can I count this pretty easy I can use an aggregation pipeline that says count but I really don't want to call it as a string you can nobody's stopping you but hey I don't want to call it as string I want to call this one as users with uh special phone number or whatever you want to call it you can just provide a better one so in this one we just saw that how easy it is once we know how to do the grouping and how we do thep uh matching as well so pretty nice and pretty easy couple of question we learned in this one so go ahead and practice them again how many users with the MN as one of their tags how many names ages we learned a lot about project so go ahead and study a bit about the project and the match I just love to call it as projection but anyways I hope this has this video has given you a little bit more in depth and you'll be able to answer similar kind of questions uh when you're building your own pipelines as well that's it for this one let's catch up in the next video all right so probably this is going to be the second L section or probably the second last video if you're watching it in the playlist in this video we'll be answering four or probably five I don't remember exactly four or five questions uh that will help you to understand a little bit more about how to build such kind of aggregation Pipeline and then I'll walk you through how you can study more it about it from the documentation itself I've tried to cover up all the scenarios which you will usually face while building any analytics or maybe an admin dashboard and that's where majorly these aggregation pipelin are used and for this video also we'll be using the same database but to understand something uh more we will cover up that in the next video that is the lookup and that requires actually two uh collections simultaneously and that's why we have those authors and books so that's the plan that's the strategy for this in the next video let me share the screen and one by one we'll go through with all the other questions uh that we have around so I'm going to go ahead and bring up the next question that we have so the next question that we have is who has registered most recently now with this uh some things should come to your mind automatically by this point that you have learned so much with the aggregation pipeline so who have registered most recently uh obviously we have to make this query a little bit more useful because it just says who has registered most recently that means just get one user uh probably let's just get four users who are recently registered so first first and foremost this is clear that I need to sort the entire collection uh with one of the property which is registered at so let's just see how we can do that so if I go back and again we can just remove all these exercise that we did and uh let's see so we have these options up here so notice here there's a date known as registered uh depends on how you have actually injected the date in the mongodb that's also very dependent but it's not really a big deal we can have our aggregation Pipelines so this is my first Pipeline and all I have to do is use the Sorting feature so sort and what field you want to sort and what ordered I want to sort based on registered registered or registered at let me just check that okay so this seems like registered so I'll just copy this so that I don't make a typo okay so this is registered and what is the sort order I'll just say ne1 so that gives me the whole set of document uh being being sorted based on register so negative one means the user who have registered in the very last will come at the very top that means latest registered okay uh what can I do we can add another pipeline to find out uh that how many people you want really to get from this pipeline let's use limit for that uh so we're going to go ahead and say hey let's use a limit in this one and I'll say give me four five however you wish and then on top of this this obviously will give you the four users that we have so we have four documents but I don't want to give this entire details of that probably I want to give uh name I want to give the registered information and maybe favorite fruit as well don't know why but I just want to give that back so this can be done in your next stage of the pipeline and this one I'll just say hey I want to project so how the projection looks like I'll just give that so what is the projection uh looks like I'm going to give a name uh that's going to be be one uh I'll also give the registered registered to be one and what else I'll give the favorite fruit that will be one so everything needs to be one of course with a comma and now we have the information which gives you ID of course uh name registered and the favorite fruit so we have choked down the information of whoever whoever has recently registered top four values of it and now we are actually giving only the limited information so you can see although the question that was asked in this one was really simple but we actually modified it to be much more fruitful let me go ahead and move on to the next question that we have so that we can actually answer that okay categorize users by their favorite fruit okay that's interesting because if you look at this right now let me just remove this the whole documents in the mongodb database are actually not categorized they are each one of them have their unique ID value which mongodb automatically generates uh this underscore ID field and that's what it is right now they are not grouped based on anything so yes you get that the answer is here we need to actually categorize them or group them together the reason that's the reason why I kept this question because to make you understand that categorization grouping them is almost same categorize the user by their favorite fruit so we need to group them okay uh so one thing is clear I have to use the grouping so let's just say group but how do you want to group them together hm this is where things get interesting I want to group them based on their favorite fruits so this is their favorite fruit so if I go back here I know that there is a favorite fruit favorite fruit favorite fruit here it is I'll copy this now the new ID that I want to generate if I just say null that means group them all together but if I just want them group based on a particular field all I have to do is just remove this have this one and this needs to come into dollar so obviously you know that part now this is a field value so now all of them are grouped based on favorite fruit so we have bananas apple and strawberry so these are our categories of fruit now but we don't know how many users are there in each one of them so we need to work a little bit more on that uh how many users are there so let's what we can do now is push more values they're grouped but they are not usable so let me show you something more once you're done with this what you can do is we can actually use something like this I can say users in this one I'll show you what this is doing and here I can use another U variable not operator of the mongodb where you can use actually dollar push okay again notice push is an accumulator Ah that's interesting we saw that average some some of them are accumulator push is another such accumulator what does it do I'm going to put up a variable field name here so I'm going to go ahead and say dollar name okay uh hopefully that does the job yep so now we can see that what this does actually let's read that first that would be so much better to explain when I go ahead and say hey tell me about this new thing which we just studied uh which is push just a second just a second and there we go okay so the push operators appends a specified value to an array okay so one thing is clear that if I use a push operator that creates an array for me but what values it's going to add in that array whatever the value I ask it to do so so I'm saying that hey create a new array uh which is going to be users when I mentioned users it was not an array it was just a field name when I said push then it says okay I'm about to push an array but what value should go inside this push uh the array users it should be based on the field name and since it's an accumulator it doesn't really just overwrite the values it keeps on adding the values that's what accumulator does and now we can see we have an array we could have counted the document yes that could be done but let's just say uh for some reasons I want to Loop through all the values in an in input box or maybe in a card that hey all the people who love Apple who love strawberry or something so this is another use case I can just go ahead and see all the values up here uh this uh could be further sorted if you wish uh you can actually use ascending descending that's totally up to you but we have actually categorized all the users and we have learned a little bit about these new things as well uh pretty interesting a lot more knowledge how I'm pretty sure these all knowledge will build accumulate and will help you to understand a little bit more okay uh what is the next thing that we want to have this one is interesting how many users have ADD as their second tag in their list of tags okay this is interesting why this is interesting uh because we have seen that we have tags as a list of array but all these arrays have different values at different position and this time I have a specific use case to find out that I want want all the list of tags but for only those people who have ADD as their second tag so now I'm referring myself that hey I'm restraining myself about the position of an array value as well so how can I do this it's actually super simple first of all the thing that should click to your mind is how many users so it's not about grouping together it's a condition that I have to match and whenever there's a condition to match that's all about matching that's a filtration process so first thing that comes to my mind is I'll do matching okay let's go ahead and remove all of this and I have to use match all right uh what is my query for matching the query for matching is I want to find the tags but not any tags I want a specific position in the tags which is ADD so how can I do this uh this is again a query really simple query you don't have to worry in that part all you have to say is I'm looking up for into the tags but is it the tags that you're looking up for uh no not all the tags but since since tag is an array and you can do this in the match I can say at dot one yes this this is something that you can do at the first position I'm looking up for something so what you looking up for I'm looking for ads uh yeah this is really nice and since this is a match it will automatically look for particular field and what the value of the field you're looking up for you don't have to put an dollar or anything that's that's the syntax of this so by the way in case you anytime feel that hey I'm missing out on the syntax I know what I have to use there is absolutely no harm in going into the docs again I do that all the time still when I'm writing these aggregation pipelines and all these things there is no harm in going into the documentation and figuring out how the query actually works uh how you can put the Expressions aggregation expression and all of that uh find any difficulty in that go ahead read it go ahead use chat GPT the whole idea is the job should be done okay now I'm matching that hey how many tags are there so it's giving me uh no preview why why is it giving me let's try the stage and I'm saying tags one tags do1 should be matching up to x.1 and ads my bad it's not ads it's add probably H yeah there we go so now I can see all the values that are there ads is at the position one so uh that makes it nice okay next thing is I want to find out how many documents you were able to find because that's the next question how many users so how many documents you are able to find I'm pretty sure that's the easy one for you okay all you have to do is now count so let's go ahead and say hey count you can call it as a string but as you know we don't call it as a string we call it as second second tag as ADD not not a good one but second tag add yeah that that's that's a decent name so we have 12 folks or 12 documents which actually does all of this so really nice good question we actually catered another scenario that is there all right okay uh next up is also very similar of a question but let me just go ahead and work with that next one says find users who have both NM and ID as their tag oh that's interesting I want to find both of them so uh we have seen previously in couple of examples that you can use match and whatnot and all of this but this time the scenario is different find all the users who have both NM and ID as their tags so I know in the match I can directly look for the tag field okay but I want to see that they should have both ANM as well as ID so this is the point where you learn something new so if you go ahead and search for this another uh operator known as all let's search this and let's see how does that one performs so there's the all okay this one says the all operator selects the document where the value of field is an oops why are you going there is an array that contains all the specified elements so all says I will select the document if all of the criteria that you give me fulfills H interesting this is exactly what my problem is I want to find all the users I can do that through the match but I have to provide a condition there a query which selects inside the tag M NM and the ID maybe more so let's go ahead and try this out okay let's write this query again and we'll say hey let's remove this my first I know that this is going to be match but what is my query in the query I have a tags field so I'll just go ahead and say hey this is my tags okay what do you want in the tags to happen you can directly answer that that's okay as well but in this one uh you could have directly said it like this that I want the tags should be like that or something but in this case we want to write a specific query on top of this so I'll just use the curly braces again now why curly brace because I want to use another operator into this one so I'll just go ahead and say I want to use an operator all okay how does all works you have to provide an array and all the values that you're looking up for just add this so this one is NM and what else is asking for probably IDs ID okay so I'll just say ID so now that we are matching all the documents in a specific field tag all these values are present so hopefully no document okay uh probably I'm missing something okay uh let's let's go on to the stage no what am I missing unexpected to okay did I miss the comma no probably not where did I miss the comma NM all looks okay to me let me just quickly check that where am I missing a comma found it it was not a comma actually there's an additional curly brace here uh so yeah just remove that and now we have all the tags which has all these values so pretty nice okay uh I love to going back into the stage and come back here it actually organized it much better than what I'm doing up here okay so this is really nice uh one more scenario that is uh used up here okay uh one more of the question and then we have just one more thing to study in the next video all right so let me just go ahead work with that okay list all the companies located in the USA with their corresponding user count H that's interesting let's go ahead and evaluate our database so in this database we know that there are company and the company we have email title phone number and location as well if I drill it down location further I can see the country is USA uh there is no guarantee that some of the company might be listing as United States of America so there could be another case for that but I'm not worried on that I'm assuming that you have given them a drop down and they there are not thousands of options available like that so this is what we have to do now obviously we have to select all the companies who has loc who are located in USA so I know that I have to 100% go with the match and once I am in the match then I have to select uh a company location Country USA why I said that uh company location USA that's why I said it okay let's try it one more one by one how can we deal with this one okay first of all let's use match so we're going to go ahead and say match what is my query uh my query is that hey let's go ahead and select company uh who has a location and the location country is my key let's verify that one more time company uh in that we have location in which we have country okay what should be the value of this one the value of this one should be USA okay so this will do all the matching for me now obviously I have a sample of documents which is fulfilling the criteria so now I'm assuming that my source of Truth is that all the companies are in USA but how can I count this document the counting should be easy I can group them all together and based on that I can do the counting okay uh here's a couple of interesting thing that I'll walk you through so let's go ahead and get started with this one so how can you do them I can group them first of all I'll say group and now with this group I can do a couple of things I can just remove this I can just say hey I want to group all of them together so yes you can do null okay uh listen to this very carefully now I want to say that I want to have a user count name it whatever ever you like and here I want to sum all of them that's what the counting actually does give you all the unique values and you can use a dollar sum and sum is an accumulator which you increase by one as soon as you find that particular thing okay now notice here it says ID null and the user count is 255 although we are getting our user count but this ID null is actually grouping them all together now the great thing about this is that actually you can group them all together based on the company title as well if you wish if you wish otherwise if you don't want to that's totally up to you you can provide more Fields here so just one of the field uh in case you want to have it I'll just say that hey you know what I'll use this one and I'll say company title so once I do this notice here we get the company as a unique ID so every company obviously uh the user count is one for each one of these company we have limited value uh so this query was a little bit vague that's why we have got this one list all the companies located in the USA and their corresponding user count so the query was a little bit tricky first of all found all the users who are look all the companies which are in USA we did that in the first stage and with their corresponding user count so we want a unique count of the users of that company not all the companies what we did with the previous one so let me show you what we did when I said null here what you were doing is uh finding out how many total number of people are there in the US companies we don't want that we want specified company wise data for that and that is the reason why we did the company. title so group them together based on the company title now right now it's showing only the 10 documents because it's a sample only uh but again you can see there's a whole data that that comes up here so it's little difficult but yeah we have definitely more than 10 sample of 10 but you can get the idea all right so quite a lot that we have studied and the whole idea behind giving you these questions is not to teach you in a boring way uh which simply gives you the idea about hey this is all this is all so I've tried my best to give you the scenarios and the situations because most of the people who learn the agregation pipeline they don't learn it line by line that hey this is the documentation of aggregation pipeline just go through it line by line and just study all of it no they want to write a query uh based on what is being asked to them while designing the admin dashboard or analytics or something like that so we have covered almost all of them and the stages I hope this whole tutorial have given you a little bit more of the idea in the next video we will learn about one more of the query which is little bit difficult to understand at first but that's the whole Meat part of the entire aggregation Pipeline and I'll give you a heads up for that I'll be working with the lookup oops an extra L there does it does the search for me nah doesn't do a good search for me not to blame them I I messed up so look up aggregation this is the one where we'll be going through this is almost like a left outer join you don't have to worry the whole idea is we'll be joining the two documents together uh then we'll be using a little bit more on top that so I hope uh you'll study a little bit more on the documentation side of it uh let's catch up in the next video and finally finish up uh the fin uh video on the aggregation pipeline that's it for this video let's catch up in the next one all right welcome to the final video of the mongod DB aggregation pipeline as of now uh so it really feels great that we have reached so far and now we are able to think from the pipeline perspective and not from the memorizing that hey there's a group there's a match there's all no we actually learned everything from the scenario perspective okay moving further one of the last scenario in fact that could be many but anyways one of the last scenarios that you'll be using is the lookup now what is this lookup it is very common to have a mongod DB structured which is very familiar to the squiz databases as well and sometimes you want to perform some left joints onto them basically you want to just uh Ed some other fields into the other documents based on the one one field now there are couple of nuances that comes with that I'll show you the solutions of that and how I found couple of solutions for them while working in the real world production apps so I'll walk you through with that so let me share the screen again all right so uh I hope you can see that we actually worked on couple of more things we have authors we have books and users so far we have been into the users only but now let's check out the author the books first in the books we can see we have ID we have title we have author undor ID which is 100 by the way these are not mongod DB ID just to make sure you understand things clearly I've changed the IDS and the Jenner is genre by the way J the genre is classic and we have more similar kind of things if I go back onto the authors remember there is a 100 ID as well the ID 100 stands for F Scott uh the birth year is 1896 so how can we actually use all of this to perform some of the aggregation pipeline now I don't have any questions as such say specific I just want to show you how the lookup actually works that's a really powerful aggregation pipeline let's go into the books and let's go into the text area to write that and here we go okay so what do you want to do uh in this pipeline the first thing that I want to do is look up look up now this lookup has couple of fields that you have to always always provide this time we'll not just remove all of them the first one is from from which collection uh I want to look up the data then the second is what is the local field in the current field where you are this is my local field foreign field is where that field exactly match because this is the matching that goes on and finally what do you want to call the result as uh just to give you one more piece of information because now that you have seen let's analyze the data again so if I look at the books uh there is an author uncore ID which is my local field and based on this local field I know that there is a foreign field as well what is that foreign field the author ID that I'm storing is 100 if I go into the authors look at this very carefully the underscore ID is 100 so these two fields are actually connected to each other so for the books this underscore ID is a foreign field and the information that you want to get so let's go back into the books and take a look on the aggregation pipeline again let's go into the aggregation and I want to go into the text side come back here this is my aggregation pipeline we know that we want to use a lookup uh what is the collection that you're calling I'm calling my collection as authors okay uh so this is where I want to look from okay once you have done this uh then what is the local field that you have my local field is known as author undor ID so this is the field that I'm storing this is local for me what is the exact equivalent of this field in the another collection so this is where the foreign field comes up in the foreign field we are calling this as underscore ID yes I explicitly called it as underscore ID there is no such need of calling it feel free to name this field whatever you like and the final is what do you want to call the result as because obviously you now you have collected the information what do you want to call this so I would love to call this one as something like author uncore details so now if you look at this now we have this another Field in inside the same document which is author uncore detail which is an array this is something really worth looking now notice here this is an array who has further down an object in which you get the underscore ID name birth here now this is a little bit tricky to work on with especially if you are having a front- end team now the front end team will question you that why did you store it this way the arrays and object they don't know that we actually brought this from a pipeline but they'll say could you do a little bit better job on this one that author detail it's just one value so why are you doing all this now this is a little bit tricky to answer uh to the Fronton guys because this is how it looks because right now we are having just one value but there could be more values so that's why the all the answer is always result but we can actually modify this by adding or chaining one more pipeline to make a life a little bit easier and to just give them only the author details so how can I get this or probably they don't want all these things they just want a name that's all what they want so further down this road once you have done the lookup now it's all there now let's learn a little bit more I'll say that I want to use another of the operator which is known as ADD Fields now what this add field does uh it actually adds field and you can calculate those fields I'm going to call this one as author details okay how this expression will be evaluated to evaluate this expression I'll use uh a little bit of these guys I have to remove these ones and I'll actually go ahead and hit an enter here so how do you want the author details to be calculated I'll just say that I want to grab notice this carefully I'll just say dollar and then I'll say first now notice here this first what it does it can go through into any array and bring out the first Value First value out of it now notice here so far when we we are doing this author details this array only has one value so I can just go ahead and grab that first value uh there could be a case where there could be more values in this array but I can just use this first I'll show you the alternative as well so I can just say first very very weird operator in itself and then what do you want to call this one so that you have to mention as well so I want to call this field uh like you have to tell from where you have to bring in and this one is in author details so I'll just say dollar author uncore details all right so now that you can see this author detail now is an object because we have pulled the data from the array and now this is directly uh yes much better compared to having an array and loop through that finding the value much much easier uh that anybody can say author detail author uncore details. name yep much better but can we do better I think we can do better we and not better but there is an alternative of doing that you can use first but I'll show show you something similar to that that is also being used let's just say I don't like this structure the syntax of first it's a little bit weird then what you can do is you can use another elaborative option which is known as array element at in this one you can specifically mention from the array which element from at which position I want to bring that in So when you say array element at I actually prefer that the syntax is weird again you have to provide an array for this one so yeah this is the way and first parameter is give me the field and then provide me what index should I pick up not really that difficult so I'm going to go ahead and say dollar author author uncore detail and this parameter I'll pick up the zero value of it array starts from zero no big deal there uh does the same job as we have seen but you will find that this array element is being used quite a lot in the production because it makes code much more readable understandable but fundamentally there is no difference when you use something as first or when you use a element at I just wanted to show you both the elements and both the ways of doing the things now there could be more uh you can provide more of these ad fields and whatever you wish again nothing wrong nothing right in this one these are just situations so I thought let's give you these situations so I think now this actually makes a lot more clear of how these lookup and AD Fields works I'm pretty sure this entire series uh might have given you a lot of insight about how to use mongod DB aggregation pipeline I understand this was a bit of an advanced tutorial and this might need a couple of walkthroughs back and forth again to understand more of it if you have more of such questions just drop down in the comment section that hey this is more situation this is more scenario and I'll try to uh give you that how will I write the pipeline for that and maybe we can make more videos on onto the same uh Journey so that is it for this video I hope you have enjoyed this I enjoyed thly uh preparing for this entire material and writing these questions having these Pipeline and making this tutorial more of a discussion rather than just the instruction I hope you have enjoyed that if yes uh do drop me a note on Twitter if the series was helpful for you uh a motivation on Twitter LinkedIn anywhere is good for me I really need that that's it for this video let's catch up in the next one
Info
Channel: Hitesh Choudhary
Views: 42,439
Rating: undefined out of 5
Keywords: Programming, javascript, mongodb, aggregation, mongodb pipelines, database, NoSql
Id: vx1C8EyTa7Y
Channel Id: undefined
Length: 118min 14sec (7094 seconds)
Published: Wed Dec 27 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.