Naan Mudhalavan Frontend Development Session - 10

Brief Summary

This session is a comprehensive guide to MongoDB database administration with a focus on CRUD operations (Create, Read, Update, Delete) and aggregation. It begins with a recap of previous lessons, including inserting single and multiple documents, and then transitions into more complex operations such as finding documents with specific criteria, updating data, and deleting documents. The session also covers how to use MongoDB Compass, create a cluster, load sample data, and connect the cluster to MongoDB Compass. The latter part of the session is dedicated to aggregation, explaining how it improves query performance and allows for data processing, including creating, removing, and adding fields.

  • Covers CRUD operations (Create, Read, Update, Delete) in MongoDB.
  • Explains how to use MongoDB Compass and connect to a cluster.
  • Details aggregation, including stages like match, project, group, sort, limit, and skip.

Introduction and Recap

The session starts with a welcome message and a brief overview of the topics to be covered, which include front-end development with React and MongoDB database administration. It is mentioned that this is the final session of the series. The instructor then recaps what was discussed in the previous class, focusing on insert operations such as insert one and insert many.

MongoDB Compass Setup

The presenter guides viewers on how to download and set up MongoDB Compass, a GUI for managing MongoDB databases. To download it, one needs to search for "MongoDB Compass" in Chrome and click on the download link. After downloading, users can open MongoDB Compass and connect to their database by providing a name and color for the connection, then clicking "Save and Connect." Once connected, users will see admin, local, and config databases. The shell option allows users to execute commands. The presenter explains how to switch between databases using the use command, and how to display all databases with show DBs. To see collections within a database, the show collections command is used.

Find Operations and Projection

The session covers the find operation, including find one and find. The find method returns multiple documents, while find one returns only the first document. Projection is introduced as a way to specify which fields to return in the query results. For example, to return only the student ID and name, you can use db.collectionName.find({}, {studentID: 1, name: 1, _id: 0}), where _id: 0 removes the default ID field from the results.

Operators in Find: AND and OR

The presenter explains how to use operators such as AND and OR in find queries. For an AND query, multiple conditions are specified within the same document. For example, to find students whose age is greater than or equal to 22 and have a grade of B, the query would be db.students.find({age: {$gte: 22}, grade: "B"}). For an OR query, the $or operator is used with an array of conditions. For example, to find students whose age is greater than or equal to 22 or who have a grade of C, the query would be db.students.find({$or: [{age: {$gte: 22}}, {grade: "C"}]}). Projection can also be used with these operators to specify which fields to display.

Update Operations: Update One and Update Many

The session transitions to update operations, starting with updateOne. This method updates a single document that matches the specified filter. The syntax is db.collectionName.updateOne({filter}, {$set: {updates}}). For example, to change the grade of a student named "if" to "C", the query would be db.students.updateOne({name: "if"}, {$set: {grade: "C"}}). The result indicates the number of documents matched and modified. The updateMany method updates multiple documents that match the filter. For example, to change the grade of all students whose age is greater than or equal to 22 to "A", the query would be db.students.updateMany({age: {$gte: 22}}, {$set: {grade: "A"}}).

Delete Operations: Delete One and Delete Many

The presenter discusses delete operations, including deleteOne and deleteMany. The deleteOne method deletes a single document that matches the specified filter. For example, to delete a document with the name "if", the query would be db.students.deleteOne({name: "if"}). The deleteMany method deletes all documents that match the filter. For example, to delete all documents where the age is greater than or equal to 22, the query would be db.students.deleteMany({age: {$gte: 22}}). An empty filter in deleteMany will delete all documents in the collection.

CRUD Operations Explained

The instructor summarizes the CRUD operations in MongoDB: Create, Read, Update, and Delete. Create involves inserting new data using insertOne and insertMany. Read involves reading data using find and findOne. Update involves modifying data using updateOne and updateMany. Delete involves removing data using deleteOne and deleteMany.

Introduction to Aggregation

Aggregation in MongoDB is introduced as a pipeline of stages to process data, improving query performance. It is primarily used for read operations, allowing for data transformation, creation, removal, and addition of fields. Aggregation is used to process data, while indexes are used to improve query performance.

Setting Up MongoDB Atlas and Loading Sample Data

The presenter guides viewers on how to create a cluster in MongoDB Atlas, a cloud database service. The steps include creating a free cluster, selecting the M0 tier, and creating a database user. It is also shown how to add an IP address to the network access list to allow connections from anywhere. The presenter explains how to connect the Atlas cluster to MongoDB Compass by copying the connection string, pasting it into Compass, entering the password, and saving the connection. The process of loading sample data into the cluster is also demonstrated.

Aggregation: Match Stage

The session covers aggregation, starting with the match stage. The match stage is used to filter documents based on specified criteria, similar to the find query. For example, to find all sales in the store location "Denver", the query would be db.sales.aggregate([{$match: {storeLocation: "Denver"}}]). The countDocuments method is used to count the number of documents that match the criteria.

Aggregation: Project Stage

The project stage in aggregation is similar to projection in find queries, allowing you to specify which fields to include or exclude in the output. For example, to display only the purchase method field, the query would be db.sales.aggregate([{$match: {purchaseMethod: "Online"}}, {$project: {purchaseMethod: 1, _id: 0}}]). The toArray() method is used to return all documents at once, instead of a cursor.

Aggregation: Group Stage

The group stage is used to group documents based on a specified field and perform calculations on the grouped data. For example, to find the number of sales in each store location, the query would be db.sales.aggregate([{$group: {_id: "$storeLocation", totalSales: {$sum: 1}}}]). This groups the documents by store location and calculates the total sales for each location.

Aggregation: Sort, Limit, and Skip Stages

The session covers additional aggregation stages: sort, limit, and skip. The sort stage is used to sort the documents based on a specified field. For example, to sort the store locations by total sales in descending order, the query would be db.sales.aggregate([{$group: {_id: "$storeLocation", totalSales: {$sum: 1}}}, {$sort: {totalSales: -1}}]). The limit stage is used to limit the number of documents returned. For example, to get the top three store locations by sales, the query would be db.sales.aggregate([{$group: {_id: "$storeLocation", totalSales: {$sum: 1}}}, {$sort: {totalSales: -1}}, {$limit: 3}]). The skip stage is used to skip a specified number of documents. For example, to find the third highest store location by sales, the query would be db.sales.aggregate([{$group: {_id: "$storeLocation", totalSales: {$sum: 1}}}, {$sort: {totalSales: -1}}, {$limit: 3}, {$skip: 2}]).

Q&A and Conclusion

The presenter opens the floor for questions and answers, encouraging students to post their doubts in the live chat. The session concludes with a thank you message and a recap of the topics covered.

Summarize any YouTube video in seconds

Get summaries like this one for any video, in your language, with the free Summ app.

Download on the Apple Store
Get it on Google Play

Transcript

Full transcript of “Naan Mudhalavan Frontend Development Session - 10”. Click a timestamp to open the video at that moment. Need another video? Use the YouTube transcript generator.

38:00 Yeah. Hi all. Uh very good morning. I welcome you all to the front end development with React and MongoDB database administration session. Uh I hope this is the last session. Uh so let us wait for 5 minutes. So we'll start the session at 11:5 116. Thank you.

43:36 Yeah. So let us begin.

46:56 See please uh you recap what all we have uh discussed uh in the previous class. Insert. Insert one. Insert many.

49:41 I hope everyone downloaded this uh MongoDB compass. I hope everyone downloaded this MongoDB compass. If you have not downloaded uh please open Chrome, search for DB Compass and click on download. Here you can find this MongoDB compass. Download this

50:35 Please download this MongoDB compass.

53:22 So after downloading this MongoDB compass come back uh open your MongoDB compass you can see and if you are opening it newly you can see directly you'll get a pop-up like this. Okay. Then you click uh give something any name for it. Font give color and click on connect. Save and connect. Okay. Excuse me. Then you can see admin local and config. Admin local and config only. Okay.

54:08 So you can see see an option here. Click on this. Click on this option. This is a shell option here. Open in shell it is. Or else you can find here also. Fine. Now so whatever it is showing here that means you are in this DB. That means you are in this DB. Okay. Now, now if you want to switch to any DB, you have to use this command use M. If if you want to use any other DB

54:56 which is not present. So you can directly write the name whatever DB you want to use. like if I'll write smart bridge and I'll use it. Okay. So yes uh in previous class I have created this enm uh DB. So I'll get into it. Okay. And what is the command to show all the DBs in your uh connection? It is show DBs. Okay, show DBs. So these are all the DBs present in your local. Okay, if you want to see the collection in NM database, you can type

55:41 show collections. Students is only the one collection present in DV. Fine. Fine. Now in the previous class we have seen how to insert how to insert right we have inserted many documents insert one document insert many we have seen now let us see find let us see find so find also I've told you that we have two two methods for find one is find one and one is find so what will find one return and what will find uh will return we'll see DB dot

56:29 collection name dot fine. So fine will return multiple documents and will return more than one documents but whereas

58:39 Okay. So now let us see what will find one return. See find one will return the first document only one document which is the first. Okay. Fine one will return only the first document. Okay.

59:26 Okay. So now let us see something called projection in find something called projection. So what is projection actually? See if I uh type this find I'm getting these many documents with these many fields. Okay. Okay. We are uh getting these all the documents whatever we are finding with 1 2 3 4 5 six fields you are getting for suppose you think I don't want some fields. I want to return only uh name

1:00:14 and the student ID with it with age. Okay, name student ID and with age. So there's something called projection. So there is something called projection. In projection what you have to do is you have to db dot find sorry or collection name dot find inside find you'll have two parameters. Okay. Inside fine you'll be having two parameters. Okay. One is the filter. So let me clear this. So write db dot students

1:01:02 dot find. So here you'll be having two parameters. Okay. This is the filter. Okay. This is the filter. So this is the filter and this is the projection. Okay. So what you'll write in filter you can write whatever you want. But in projection what you have to do is you can specify only what all uh fields you want to you can specify only what all fields you want to uh return.

1:01:49 So I want to return student ID and its name. So what I'll do I'll give student ID col 1 comma name colon 1. So you can see I have returned only student ID and the name. So underscore ID field is mandatory. I mean uh in projection it will be one. If you want to remove that you can here you can specify underscore id colon 0. Okay. Now you'll get Now you'll get only the

1:02:57 student ID and name because we have given projection here. Okay. We have given something called projection here. Understood. Please make a note of this. Please make a note of this.

1:06:46 So now let us move on to few operators in find. Few operators in find. So db dot students dot find inside fine. What we'll do is we have a document here. What shall we find?

1:07:56 Okay. Now let us there's no query here. Now what we'll do is let us find uh [Music] let us find those students whose age is greater than or equal to 22 and also having a grade B. Fine. Sorry. name age colon dollar greater than or equal to

1:08:43 31 and grade B. So only Bob Let us see is it is only Bob 20. What what age we have given? We have given greater than or equal to 21. 21 Bob will come. Okay. And grade is B. 21. Charlie will come but grade is A.

1:09:29 That is why this this was not written. 23 will also be qualified for first filter but grade has been rejected. So um uh this is this will also not be considered because age is not there. So this is how you can use two or more more than one conditions like this. Okay.

1:10:42 Fine. This is and please make a note of it. This is and okay this is and so now let us work with or. So in the same query what we'll do is I want to find the students whose age is less than greater than equal to 22. Give me a minute guys. Okay, sorry guys. So,

1:11:28 uh we will find the students whose age is greater than or equal to 22 or who have C grade. Okay. So we will write db dot students sorry use dot find inside find we will use or operator because what we are asking is what we are asking is to we have given two conditions those are or having one at least having one at least right either age should be greater

1:12:14 than 22 or grade should be C so or let me open a square bracket and one curly brace here I'll give age colon H colon greater than or equal to what we have seen H than or = to 22. Right? Greater than or equal to 22. And here again open one more thing. What we were saying it is grade right? Grade colon capital C.

1:13:00 See this is the query find using or operator inside a square bracket taking multiple conditions taking multiple conditions. Okay sorry one is age greater than or equal to 22 and one is grade whoever is having seat only Diana. So let me exchange this to B. Yeah, I've got two documents now. Age is greater than or equal to 22. This is satisfied. And also B is also there. Okay, fine. Here age is greater than or equal to 22 is not satisfied. But grade B is satisfied.

1:13:48 Okay. Let me give 21 years fine. This is or so we'll use projection in this only. I'll tell you how to how to use projection in this. So here at the last you can specify the student whatever fields you want to display student ID it is right col one

1:14:36 student ID col one and name one comma ID colon zero So, so this is called projection. This part is called projection. Okay. This is the query and this is the projection. [Music] Okay, understood. Please make a note of this quickly.

1:16:18 So now let us come to update query. Let us come to update. So what we will do is um in update let us write a query read update. Okay.

1:17:38 H. Okay, we'll find CR A B A C B A B A C D. Okay, we'll find this students document and we'll change the grade to C. Okay, so the student anything you can find with the student ID or this. Okay, student ID or this is the name. So like we we have two parameters here also. We have two uh parameters in this also. So db do.colction name dot

1:18:25 update one. Instead update one you'll be having two parameters. One is the filter and one the update. Okay. One is the filter and one is the update whatever updates to be done. So first you have to filter the document right. So we'll give name if right sorry sir if let me confirm yeah name if oh okay name if and I want to change the students grade to C. So, dollar

1:19:14 dollar set colon grade to C. Okay. Enter. So, you can see uh some things here. Acknowledged. True. This uh has been executed and it has found one document and it has modified one document. Okay, it has found one document and it has modified one document.

1:20:03 So now let us go here and refresh and see grade it has updated. Now if you run the same query here again see what will come matched count one but modified count zero because it it has already up updated its document to grade C. It has already updated it document to grade C. Understood? So please take a note of this. Please take a note of this.

1:22:59 Okay, this is about update one. So, uh we'll see one more example for update many. Okay. Uh see So what update we'll do for many? So we'll find whoever age is whoever age is uh greater than 21 will change their grades to.

1:23:46 So 1 2 3. So what we will do Whatever age it is, we'll take 20 two. Okay, we'll take DB dot students

1:24:35 dot update many. We'll write a filter whose age is greater than or equal to 23. 23 22. We'll update their document. dollar set grade colon.

1:25:22 [Music] Just beat C. So we'll change the grade to A not C. Anything you can give. Anything you can give. Okay. Enter. You have seen math count two. It found two documents with age greater than or equal to 22. Age greater than or equal

1:26:07 to 22. Then it it has modified the documents. It has modified the documents accordingly whatever we have set. So let me show you. So this documents is greater than equal to 22. This grade is B. And this document the fourth document grade is C. Now when I refresh it has become A. And here also in the fourth document it has become A. Understood? This is about update many. This is about update many.

1:29:10 Okay. Now let us see delete. So now let us see delete. or how to write delete db dot collection name students do delete one. So what we will do

1:29:56 in delete one you have to write a filter. So let me delete this document with name e. So here what are you name if acknowledge to deleted count one. So you can see here here you have five documents has become four. Okay, it has become four. Now let us delete multiple documents. If I write db dot students delete any and if I give empty this thing it will delete all the documents but we'll give some condition will

1:30:41 delete all the documents whose age is greater than or equal to 22. So here we can write age greater than It has deleted two more documents. There are only two documents. Okay. Now if I write same query with empty filter without any filter filter let us see two more documents also has been deleted there is no documents in this collection

1:31:27 now so this is about delete this this is about delete Please make a note of

1:35:16 So till now whatever we have discussed this is called CRUD. This is CRUD. How CRUD? You have created, you have read, you have updated and you have deleted. So this is CRUD. Okay. If anyone ask you what is cred operation in MongoDB.

1:36:14 So you have to tell this is CRUD. CRUD means create, read, update and delete. Okay. Give me a minute. This create, read, update and delete. Okay. Create, read, update and So create means you have used insert to insert you have created new data right

1:37:01 using insert one and insert many and you have read means that means you are reading the data using find find one find find one update and delete. So these are this is the CRUD operation. These are the CRUD operations in MongoDB. Right? So now we have something called aggregation in MongoDB. So what is aggregation? Aggregation is a pipeline. Aggregation is a pipeline of stages you know to process whatever data you want. Okay, that means you will query

1:37:52 you can give a large query. Okay, you can give a large query in that. So it will in using aggregation it will improve the performance of your query. Okay, and you can process the data however you want. You can create fields, you can remove fields, you can add fields temporarily. Okay, this is only read. This is aggregation is all comes under read operation only. There is no right in aggregation. You have to uh few write operations in aggregation. But most probably aggregation is used only for maximum for to read operations only to improvise query performance. Okay, we use aggregation to improve query

1:38:38 performance. No, no, we use indexes to improve uh improve query performance. But in aggregation, we use we process the data. We process the data however we want. Okay. So let us see aggregation in MongoDB. We need a sample data. Okay. Give me a minute. I'll get some sample data.

1:41:36 So you'll be having I'll disconnect this connect this project zero cluster. So you have this all sample data right from where you will get the sample data is

1:43:01 In your MongoDB Atlas, you will find one option close sample data. Wait. So I have already loaded sample data that is why I guess is not showing. Let me confirm it once again. Huh? Here you have see you can you'll have this option here to load sample data set in your MongoDB

1:43:48 cluster. See students who have not created their cluster. Let me show you.

1:45:06 to please uh listen carefully how to create a cluster. I'll show you. See, I have to go to cloud.mongodb.com. If you have created a cluster, it is fine. Well, and good. If you have created the cluster, the UI will be like this. You'll get a cluster zero name and project name everything. But if you haven't created a cluster, it will show you like this. So click on create. Just a simple steps. That's it. Select this free cluster. Select this free cluster. My screen is shared. Right? My screen is shared. Okay. Select this free cluster here. M10 M0. Come down

1:45:54 create deployment. Just do nothing. Select this and click on create deployment. So I'll remove this Click on create database user and choose a connection method and you can close it. Okay. And now your C cluster has been created. Your cluster has been created.

1:46:42 So here you can find uh see load sample data set. So I clicked on load sample data set. My uh sample data set is loading. Till then you have to wait. You have to open your

1:47:27 Okay, you can click on this browse collections here. Your it is loading your sample data sets are loading. Okay, till then you wait. Okay, so this is how you create a cluster. Just simply uh clicking on create cluster selecting the zero m0 and uh create deployment then it will ask for the username and password. What was my username? Sorry I forgot connect shall

1:48:13 my username man. Okay. So I have my username. Remember your username. Okay. If you forget your username or password, go to database access. Click on edit. Okay. This is your username. And if you want to edit password, you can click on this. You can edit password and click on update user. Okay. And also one mandatory change you have to do. You have to add an IP. Go to network access. Go to network access. Click on add IP. Can see this allow access from anywhere. Otherwise you can type 0.0.0/ 0/ zero.

1:49:01 Okay. Thank you. Oh, now I can delete this.

1:49:49 Fine. Overview. Okay. This is how you install the cluster. Okay. This is how you install the cluster. So now come back. Now how to connect the cluster to MongoDB compass? How to connect the cluster to MongoDB compass. So here you have

1:50:37 let me delete this disconnect and I'll delete this connection. I also close MongoDB compass again. I'll open MongoDB compass. So I'll click on add new connection and I'll come back to this cluster. I'll click on connect. Here you have compass. Click on this compass. Copy this string. Copy this string. Paste it here. And remove the password.

1:51:27 Give the password. option screen.

1:52:31 I hope my screen is shared. I have entered the password. You can give it start zero and give any color if you want. Save and connect. Okay, like this you can give you can connect your uh Atlas cluster to your MongoDB

1:53:16 compass. Okay, find clear. Copy the connection string. Okay, copy the connection string. Come to your compass. Click on add new connection. Paste your connection string here. Give your password. Give the name for that connection. If you want give the color, click on save and connect. Okay, understood. So now let us see what is aggregation. I hope everyone understood how to create uh uh

1:54:08 how to create collection how to create a cluster and how to load sample data sample data set is loading. Okay. Fine. Now come back to our this thing. So this is connected. Right. Now I'll open this shell here. I'll open this shell and I'll use what should I use?

1:55:06 I'll use sample supplies. Okay, I'll use sample supplies. So in this, let me show you the collections in this database. There is only sales. DB dot sales dot find one C1 document. So this is the data of one docu. Okay, this is the data of one document. So I'll show you in the GUI also. So this is the data in the each sale

1:55:52 document. Okay, this is something like in orders collection. It's like orders you know you might know. Okay.

1:57:22 So I told you we'll practice aggregation in this right right okay so I told you right aggregation um is like a pipeline of stages is to transform the data. Okay. Uh use queries you can uh cut short it and write it in this pipeline. Okay. For us uh to uh get our query performance fast. So using aggregation we'll do uh we'll

1:58:08 run few queries on this collection on this uh database. Okay. Let me clear the screen. So in the first thing let us find all the uh let us find all the documents.

1:58:54 Let us find all the sales in the store Denver. in the store Denver. So DB dot sales dot aggregate. Okay. Not find we'll be using aggregate. Here we'll be using aggregate. So we will find all the documents. So in aggregate you have to use this square bracket. inside the square brackets you have to use this. [Music] So inside this the first the stage whatever we are using is is it is match.

1:59:41 Okay, it is match match colon give the query store location. Okay. Open. Did we go in store?

2:00:26 Oh So these are the these many uh are there for the Denver location. Okay. These many are there for the Denver location. store location and deliver these many are there. So let me uh can I count

2:01:42 So for aggregate it will not work I guess. So what I'll do is db dot uh sales dot count documents and this you can write it like this store location Denver we have 1549 documents which store location Denver okay we have 1549 and documents with store store location Denver.

2:02:40 Okay. So this is about match. This is about match. So match will act like match will act like your find query only. Okay. So now let us uh DB dot sales dot aggregate. In aggregate you lose again match. We'll see uh purchase method online.

2:03:44 So this will return a cursor. Aggregate will return a cursor. Cursor is that means see you can you can see it is giving type it for more. Type it for more. So these are only few documents. If I type it again, it will give me the next uh 60 documents like this. So to uh if you want all the documents at once, you want the all the documents at once, here you have to give dot to array. So it will return all the documents at 1

2:04:31 CC 14 85 more items are there. So it will return all the document set once. Okay. So let us do one thing. [Music] Okay, this in this also we have something called project. We have something called project. Similarly like

2:05:19 uh projection in find however how we have uh seen projection in uh find here we have project in aggregation. So for the same query let me write. So this is one stage right. So here it is till here it is one stage. Now after this stage I'll write project stage dollar project. Here I'll specify the field name whatever I I need to play. So I'll display only purchase method field colon one.

2:06:09 So these many items are coming. I'll remove the underscore id field also. To remove the underscore id field, you have to give underscore id colon zero. So you can see in single array it has returned all the documents and single array it has return all the documents. Okay.

2:07:11 Please make a note of this queries.

2:12:14 Okay. Now let us go to the next stage called group. Let us move on to the next stage called group. Okay, this is not getting closed. I hope it is visible.

2:13:29 Yeah, I hope my screen is visible. Okay. Uh then let us move on to the next stage called group. So in group what we will do is we have uh plenty stores right uh store locations we have many store locations uh how many store locations do we have DB dot sales dot find

2:14:16 project only the store location 1 ID colon E nor have Denver, Seattle, London 3, Austin

2:15:01 4, New York five, San Diego six. I can see six store locations maximum six like Denver is one, Seattle 2, London 3, Austin 4, New York 5, uh San Diego 6. Other than that I cannot see any locations. Okay. Now what I need to find is I want to find how many sales

2:15:49 how many documents are there in each location. That means how many sales are there in each location. So how to do this? So to perform this we have something called group. We have something called group stage. DB dot sales dot aggregate. In aggregate we have to write dollar group colon inside group. So group will mandatorily take it are not visible group will mandatorily take and unique ID field. So what actually

2:16:37 group does I'll tell you. Okay, this ID is different. Okay, this is this belongs to only the group group and create a new ID like you have to group the documents here and you are counting it like we are grouping the documents on what basis we are grouping the document on this store location base right we have to count how many sales are done by each store so we have I have counted most probably I guess six stores are there so For each individual stores, how many location? How many sales were done? How many sales

2:17:22 were done? So for that you have to group your documents by which field you have to group your documents by store location field. So this is the syntax you write in group the unique ID whatever you are giving is the store location. Okay. So you have to put it inside the dollar symbol. After the dollar you have to give the name of the field on which you're grouping on. After that you have to calculate the number of documents. So let me create a new field here total sales colon.

2:18:07 So in total sales what should happen? You have to add increment the count for each store location. Right? If see here I have found a Denver that means one sale has happened. Again if I'm count coming down settle in Seattle one sale has happened. So again in the third document the Denver already we have counted one. So here what will happen? It will count it will increment it will add one to that previous count only. So it will become two. Now CL also the same. Here we have added one. So after coming here it will not uh create a new field. It will come it will add that

2:18:53 count itself like two. So that that what's happen here. So you have to write sum colon 1. Okay. Sum colon 1. Now let us see how many store locations are there. 1 2 3 4 5 6. Yeah, I've told you right six store locations are there. So if you want we can calculate this. Total sales in London are 794. So total sales in San Diego is 346. Total sales in Denver is 1549. Total

2:19:38 you know uh sales in Seattle is 1134. Total sales in Austin is 646. Total sales in New York is 5.1. If you want you add this 51 plus 676 51 + 676 + 1134 + 1049

2:20:25 + 346 + 794 four 5,000. Okay, you want you can count the documents. See 5,000 documents here. Talid. Okay, this is how you group your documents. This is how you group your you use group and this total fields total sales are the temporary field. Total total sales is the temporary field. Okay. Now we have something called sort also. Now what will sort now in this query

2:21:12 only what do you want is if you want to find if you want to arrange this in ascending order or descending order like whichever total sales whoever store is having uh the sales from highest to the lowest. So here the same query you can write the same query. Okay. You can write dollar

2:21:57 sort okay colon total sales colon minus one minus one means it will come in descending order that means the highest to the lowest if you give one it is from the ascending like lowest to the highest okay Denver is having total uh highest sales 1549. Then comes Seattle, then comes London, then come Austin, New York, then come San Diego. Okay, understand this is about group sort. This is about sort. Now let us see one more stage in

2:22:44 aggregation. Like if you want to find the uh top three top three. So in the same filter what you will do is in the same filter In the same filter you'll be writing dollar limit colon three. So you'll get only the top three.

2:23:32 You'll get only the top three. Okay. If you want to find the third highest, if you want to find suppose the second highest or third highest, what you will do in the same query? You have another stage called skip. Sorry. You have another stage dollar

2:24:19 skip. You have three documents. You wanted to find um the top third. So you limit it by three and skip two documents. This is the third one. The third is thing. So this is how uh aggregation helps us in fast query performance. So fast uh you know transforming the data. Okay, this is about aggregation in MongoDB. Baby.

2:26:16 So please make a note of this. Please make a note of this stage. See have everything here.

2:28:17 Students, if you have any doubts, uh please post it in the live chat. I am enabling the live chat. If you have any queries, please post it in the live chat.

2:30:16 Students, if you have any doubts, please post it in the Q&A the live chat

2:33:43 Students, if you have any doubts, please post it in the live chat.

2:37:08 Yeah. Okay. All uh thank you all for joining. I hope everyone understood uh today's session whatever we have discussed. Okay. Uh thank you all. Thank you for joining. Have a nice day. Bye-bye.

Share

More video summaries