
- 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
How we can perform sort on ObjectId column in MongoDB?
To perform sort on ObjectId column, use sort(). Let us create a collection with document.
> db.demo403.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e6f89b0fac4d418a017858e") } > db.demo403.insertOne({"Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e6f89b2fac4d418a017858f") } > db.demo403.insertOne({"Name":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e6f89b5fac4d418a0178590") } > db.demo403.insertOne({"Name":"Adam"}); { "acknowledged" : true, "insertedId" : ObjectId("5e6f89b8fac4d418a0178591") }
Display all documents from a collection with the help of find() method −
> db.demo403.find();
This will produce the following output −
{ "_id" : ObjectId("5e6f89b0fac4d418a017858e"), "Name" : "Chris" } { "_id" : ObjectId("5e6f89b2fac4d418a017858f"), "Name" : "David" } { "_id" : ObjectId("5e6f89b5fac4d418a0178590"), "Name" : "Bob" } { "_id" : ObjectId("5e6f89b8fac4d418a0178591"), "Name" : "Adam" }
Following is the query to perform sort on ObjectId column in MongoDB −
> db.demo403.find().sort({_id:-1});
This will produce the following output −
{ "_id" : ObjectId("5e6f89b8fac4d418a0178591"), "Name" : "Adam" } { "_id" : ObjectId("5e6f89b5fac4d418a0178590"), "Name" : "Bob" } { "_id" : ObjectId("5e6f89b2fac4d418a017858f"), "Name" : "David" } { "_id" : ObjectId("5e6f89b0fac4d418a017858e"), "Name" : "Chris" }
- Related Articles
- How can we sort a JTable on a particular column in Java?
- Perform aggregation sort in MongoDB?
- How to perform ascending order sort in MongoDB?
- How to perform descending order sort in MongoDB?
- How to generate ObjectID in MongoDB?
- How to convert ObjectId to string in MongoDB
- Convert string to objectid in MongoDB?
- Display MongoDB records by ObjectId?
- Perform Bubble Sort on strings in Java
- Find a document with ObjectID in MongoDB?
- How can we perform threshing and winnowing?
- How can we sort a JSONArray in Java?
- Cast to ObjectId failed for value in MongoDB?
- Create ObjectId in MongoDB using a seed string?
- How to return documents of a collection without objectId in MongoDB?

Advertisements