
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
Getting the highest value of a column in MongoDB?
To get the highest value of a column in MongoDB, you can use sort() along with limit(1). The syntax is as follows −
db.yourCollectionName.find().sort({"yourFieldName":-1}).limit(1);
To understand the above syntax, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.gettingHighestValueDemo.insertOne({"Value":1029}); { "acknowledged" : true, "insertedId" : ObjectId("5c900b885705caea966c5574") } > db.gettingHighestValueDemo.insertOne({"Value":3029}); { "acknowledged" : true, "insertedId" : ObjectId("5c900b8d5705caea966c5575") } > db.gettingHighestValueDemo.insertOne({"Value":1092}); { "acknowledged" : true, "insertedId" : ObjectId("5c900b925705caea966c5576") } > db.gettingHighestValueDemo.insertOne({"Value":18484}); { "acknowledged" : true, "insertedId" : ObjectId("5c900b955705caea966c5577") } > db.gettingHighestValueDemo.insertOne({"Value":37474}); { "acknowledged" : true, "insertedId" : ObjectId("5c900b9c5705caea966c5578") } > db.gettingHighestValueDemo.insertOne({"Value":88474}); { "acknowledged" : true, "insertedId" : ObjectId("5c900ba75705caea966c5579") } > db.gettingHighestValueDemo.insertOne({"Value":1938474}); { "acknowledged" : true, "insertedId" : ObjectId("5c900bab5705caea966c557a") }
Display all documents from a collection with the help of find() method. The query is as follows:
> db.gettingHighestValueDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c900b885705caea966c5574"), "Value" : 1029 } { "_id" : ObjectId("5c900b8d5705caea966c5575"), "Value" : 3029 } { "_id" : ObjectId("5c900b925705caea966c5576"), "Value" : 1092 } { "_id" : ObjectId("5c900b955705caea966c5577"), "Value" : 18484 } { "_id" : ObjectId("5c900b9c5705caea966c5578"), "Value" : 37474 } { "_id" : ObjectId("5c900ba75705caea966c5579"), "Value" : 88474 } { "_id" : ObjectId("5c900bab5705caea966c557a"), "Value" : 1938474 }
Here is the query to get the highest value of a column in MongoDB −
> db.gettingHighestValueDemo.find().sort({"Value":-1}).limit(1);
The following is the output displaying the highest value −
{ "_id" : ObjectId("5c900bab5705caea966c557a"), "Value" : 1938474 }
- Related Articles
- MongoDB query to find the highest numeric value of a column?
- How to find nth highest value of a MySQL column?
- Finding highest value from sub-arrays in MongoDB documents?
- MySQL query to select the nth highest value in a column by skipping values
- How to find the highest number in a column?
- Get the highest score value from a single column and the greatest from two columns in MySQL
- How to extract the first highest occurring value in an R data frame column?
- Getting maximum from a column value and set it for all other values in the same column with MySQL?
- Getting a list of values by using MongoDB $group?
- Getting index of the specified value in a SortedList object in C#
- Select nth highest value in MySQL
- Figuring out the highest value through a for in loop - JavaScript
- Getting the return value of Javascript code in Selenium.
- Get row data for the lowest and highest values in a MySQL column
- Getting the value at the specified index of a SortedList object in C#

Advertisements