CRUD Operations in MongoDB / Modify Query Results

7:13
Welcome back, team. In this video, we'll learn to return the result of a query in a specified order and to limit the number of documents in that result. To do this, we'll take advantage of two cursor methods, the cursor.sort and the cursor.limit methods. First, let's discuss more on cursors in MongoDB. A cursor is a pointer to the result set of a query. The DB.collection.find method returns a cursor and points to the documents that match that query. Cursor methods which are chained to queries can then be used to perform actions on the resulting set, in this case, sorting and limiting the number of search results before returning the desired data or information to the client. Let's see the sort and limit cursor methods in action. First, we'll return the results in a specified order using the sort method. We'll be working with a database called Training. And within that, we're going to be querying the companies collection. Let's take a look at one of the documents by using the findOne method. To do so I'll run DB.companies.findOne. Running this, we can see that we get a pretty large document with quite a few different fields on it. Some of these are numbers. Some of them being containers for sub-documents, there's just a lot going on here. We can definitely take advantage of one of the fields on this document to sort by the name of these companies. As an example, let's try to return the data for only music companies and sort them alphabetically by the name of the company. To do so, we'll go ahead and do another DB.companies.find. And in our query document, we'll only look for a category code set to music. This will ensure that of all the results that we get back, we're only looking at music companies. From there we definitely want to sort our results. However, sort isn't clear as to how we're going to sort those results. So we'll need to pass another document in order to specify that we're going to sort on the name in increasing alphabetic order. Any time that you're passing a document to the sort method, a value of 1 will specify that you want ascending order and a value of negative 1 will specify that you want descending order for that property. In this case, we're only sorting on the name property. But you can definitely sort on multiple fields at the same time by passing additional fields to this document. Let's go ahead and run our query. Let's go ahead and take a scroll through our results to see if we can tell if our data has been alphabetized appropriately. Scrolling through, we can find that we have a document with the name of a meme, and as we continue to scroll higher, we can find companies that fall earlier in the alphabet for the alphabetization of the company names. To make this a little bit easier to read, we might want to use a projection. We'll talk more about projections in one of the coming lessons of this particular unit. In our case, the use of this projection is going to make it easier to see only the name fields of the documents returned by the query. As you can see in our result, the documents have been organized by their name field. However, there's a few exceptions here down at the bottom. One of the things to know about alphabetization in MongoDB is that capital letters will be sorted first and grouped together, followed by the lowercase letters being sorted and put together. You can change this behavior using that options document in the sort method. We've seen how to sort query results using an example sorted on a string field. We can sort numeric fields or fields of just about any other type in the same way. Now let's move on to limiting the number of results returned. Limiting the number of results can enhance the performance of an app by avoiding unnecessary data processing. We used the limit cursor method to do this. Say we want to find the three music companies with the highest number of employees. We can start by writing a query that is very similar to the last example where we did a DB.companies.find, with the category code set to music. Then, in our sort, we'll want to pass a query document that will allow us to sort in descending order of the number of employees for each one of our documents. This will give us a result with the documents in the reverse sorted order of the number of employees. But we only want the top three. So let's also limit our results. Again, the limit method allows us to limit exactly how many documents will appear in the final result returned to the client. We can pass an argument for the number of values that we want to see in that particular result. In our case, we only want the top three. So we'll change this value to a 3. Again, these documents are pretty big and a little bit difficult to scroll through. So we might want to add a projection to make this, again, easier to read. In this case, we've added two fields to our projection, both the name of the company and the number of employees that they have. And in this result, we can see that the top three companies are going to be Spotify with 5,000 employees, Rhapsody with 150 employees, and Official VirtualDJ with 102 employees. Again, if you don't understand projections, not to worry. We'll definitely be covering them very soon. To summarize, in this video, we learned to sort and limit our query results. You can use the cursor.sort to return query results in a specified order. You can also use cursor.limit to constrain the number of results that you've returned. Remember, limiting the number of results can improve performance in your application by avoiding unnecessary data processing.