
- 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
Identify last document from MongoDB find() result set?
To identify the last document from MongoDB find() result set, you can use sort() in descending order. The syntax is as follows −
db.yourCollectionName.find().sort( { _id : -1 } ).limit(1).pretty();
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.identifyLastDocuementDemo.insertOne({"UserName":"Larry","UserAge":24,"UserCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5c94a2ff4cf1f7a64fa4df57") } > db.identifyLastDocuementDemo.insertOne({"UserName":"Chris","UserAge":21,"UserCountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5c94a3094cf1f7a64fa4df58") } > db.identifyLastDocuementDemo.insertOne({"UserName":"David","UserAge":25,"UserCountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5c94a3174cf1f7a64fa4df59") } > db.identifyLastDocuementDemo.insertOne({"UserName":"Sam","UserAge":26,"UserCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5c94a3224cf1f7a64fa4df5a") } > db.identifyLastDocuementDemo.insertOne({"UserName":"Mike","UserAge":27,"UserCountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5c94a32e4cf1f7a64fa4df5b") } > db.identifyLastDocuementDemo.insertOne({"UserName":"Carol","UserAge":28,"UserCountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5c94a33c4cf1f7a64fa4df5c") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.identifyLastDocuementDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c94a2ff4cf1f7a64fa4df57"), "UserName" : "Larry", "UserAge" : 24, "UserCountryName" : "US" } { "_id" : ObjectId("5c94a3094cf1f7a64fa4df58"), "UserName" : "Chris", "UserAge" : 21, "UserCountryName" : "UK" } { "_id" : ObjectId("5c94a3174cf1f7a64fa4df59"), "UserName" : "David", "UserAge" : 25, "UserCountryName" : "AUS" } { "_id" : ObjectId("5c94a3224cf1f7a64fa4df5a"), "UserName" : "Sam", "UserAge" : 26, "UserCountryName" : "US" } { "_id" : ObjectId("5c94a32e4cf1f7a64fa4df5b"), "UserName" : "Mike", "UserAge" : 27, "UserCountryName" : "AUS" } { "_id" : ObjectId("5c94a33c4cf1f7a64fa4df5c"), "UserName" : "Carol", "UserAge" : 28, "UserCountryName" : "UK" }
Here is the query to identify the last document from MongoDB find() result set −
> db.identifyLastDocuementDemo.find().sort( { _id : -1 } ).limit(1).pretty();
The following is the output −
{ "_id" : ObjectId("5c94a33c4cf1f7a64fa4df5c"), "UserName" : "Carol", "UserAge" : 28, "UserCountryName" : "UK" }
- Related Articles
- MongoDB query to get last inserted document?
- Find the MongoDB document from sub array?
- Set condition in MongoDB nested document?
- Find largest document size in MongoDB?
- MongoDB find() query for nested document?
- Filter specific values from a MongoDB document
- MongoDB query to remove subdocument from document?
- Use result from MongoDB in shell script?
- Find a document with ObjectID in MongoDB?
- Can we remove _id from MongoDB query result?
- Find which MongoDB document contains a specific string?
- MongoDB query to remove array elements from a document?
- Remove values from a matrix like document in MongoDB
- Get the top most document from a MongoDB collection
- Update a specific MongoDB document in array with $set and positional $ operator?

Advertisements