- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get element with max id in MongoDB?
To get the element with a max id, you can use the find() method. To understand the above concept, let us create a collection with the document. The query is as follows −
> db.getElementWithMaxIdDemo.insertOne({"Name":"John","Age":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c8bbce480f10143d8431e1c") } > db.getElementWithMaxIdDemo.insertOne({"Name":"Larry","Age":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c8bbcec80f10143d8431e1d") } > db.getElementWithMaxIdDemo.insertOne({"Name":"David","Age":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c8bbcf580f10143d8431e1e") } > db.getElementWithMaxIdDemo.insertOne({"Name":"Chris","Age":20}); { "acknowledged" : true, "insertedId" : ObjectId("5c8bbcfe80f10143d8431e1f") } > db.getElementWithMaxIdDemo.insertOne({"Name":"Robert","Age":25}); { "acknowledged" : true, "insertedId" : ObjectId("5c8bbd0880f10143d8431e20") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.getElementWithMaxIdDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c8bbce480f10143d8431e1c"), "Name" : "John", "Age" : 21 } { "_id" : ObjectId("5c8bbcec80f10143d8431e1d"), "Name" : "Larry", "Age" : 24 } { "_id" : ObjectId("5c8bbcf580f10143d8431e1e"), "Name" : "David", "Age" : 23 } { "_id" : ObjectId("5c8bbcfe80f10143d8431e1f"), "Name" : "Chris", "Age" : 20 } { "_id" : ObjectId("5c8bbd0880f10143d8431e20"), "Name" : "Robert", "Age" : 25 }
Here is the query to get the element with max id −
> db.getElementWithMaxIdDemo.find().sort({_id:-1}).limit(1).pretty()
The following is the output with a record with maximum id −
{ "_id" : ObjectId("5c8bbd0880f10143d8431e20"), "Name" : "Robert", "Age" : 25 }
- Related Articles
- How to get max(id) of row data in MySQL?
- Get MAX and MIN values along with their row id in MySQL?
- How to get max values for distinct elements in MongoDB
- Get MongoDB documents with max attribute per group in a collection?
- MySQL query to get max id from varchar type and the values in numeric?
- How do I get email-id from a MongoDB document and display with print()
- How to find by id in MongoDB?
- How to get a particular element from MongoDB array?
- Perform min/max with MongoDB aggregation
- How to get device id in android?
- MongoDB query to get record beginning with specific element from an array?
- How to match and group array elements with the max value in MongoDB aggregation?
- How can I get the ID of an DOM element using jQuery?
- How to use $slice operator to get last element of array in MongoDB?
- Selects the element with id="tutorials" with CSS

Advertisements