Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 5. Queries and aggregation > Aggregation in detail

5.4. Aggregation in detail

Here I’ll provide some extra details on MongoDB’s aggregation functions.

5.4.1. Maxima and minima

You’ll commonly need to find min and max values for a given value in a collection. Databases using SQL provide special min() and max() functions, but MongoDB doesn’t. Instead, you must specify these queries literally. To find a maximum value, you can sort descending by the desired value and limit the result set to just one document. You can get a corresponding minimum by reversing the sort. For example, if you wanted to find the review with the greatest number of helpful votes, your query would need to sort by that value and limit by one:


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial