
- 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 to order by timestamp (descending order) in MongoDB
To order by timestamp, use sort() in MongoDB. Let us create a collection with documents −
> db.demo737.insertOne({"timestamp" : new ISODate("2020-04-01" )}); { "acknowledged" : true, "insertedId" : ObjectId("5ead682157bb72a10bcf065c") } > db.demo737.insertOne({"timestamp" : new ISODate("2020-10-31" )}); { "acknowledged" : true, "insertedId" : ObjectId("5ead682757bb72a10bcf065d") } > db.demo737.insertOne({"timestamp" : new ISODate("2020-05-02" )}); { "acknowledged" : true, "insertedId" : ObjectId("5ead682a57bb72a10bcf065e") }
Display all documents from a collection with the help of find() method −
> db.demo737.find();
This will produce the following output −
{ "_id" : ObjectId("5ead682157bb72a10bcf065c"), "timestamp" : ISODate("2020-04-01T00:00:00Z") } { "_id" : ObjectId("5ead682757bb72a10bcf065d"), "timestamp" : ISODate("2020-10-31T00:00:00Z") } { "_id" : ObjectId("5ead682a57bb72a10bcf065e"), "timestamp" : ISODate("2020-05-02T00:00:00Z") }
Following is the query to order by timestamp (descending order) −
> db.demo737.find().sort({"timestamp":-1});
This will produce the following output −
{ "_id" : ObjectId("5ead682757bb72a10bcf065d"), "timestamp" : ISODate("2020-10-31T00:00:00Z") } { "_id" : ObjectId("5ead682a57bb72a10bcf065e"), "timestamp" : ISODate("2020-05-02T00:00:00Z") } { "_id" : ObjectId("5ead682157bb72a10bcf065c"), "timestamp" : ISODate("2020-04-01T00:00:00Z") }
- Related Articles
- How to perform descending order sort in MongoDB?
- How to order by timestamp in MySQL?
- Sort MongoDB documents in descending order
- MySQL query to order timestamp in descending order but place the timestamp 0000-00-00 00:00:00 first?
- How to arrange the fractions in ascending order and descending order?
- MySQL command to order timestamp values in ascending order without using TIMESTAMP()?
- C# Program to order array elements in descending order
- MySQL command to order timestamp values in ascending order?
- MySQL order by 0 first and then display the record in descending order?
- Sort by date & time in descending order in MySQL?
- How to sort TreeSet in descending order in Java?
- How to show records in descending order Android sqlite?
- How to sort an ArrayList in Java in descending order?
- How to sort an ArrayList in Descending Order in Java
- Sorting string in Descending order C++

Advertisements