0.4 C
London
Friday, March 14, 2025
HomeMongoDBAggregation in MongoDBMongoDB: How to Sort Documents By Date

MongoDB: How to Sort Documents By Date

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following methods to sort documents by a date field in MongoDB:

Method 1: Sort by Date Ascending

db.sales.find().sort({"date_field": 1})

Method 2: Sort by Date Descending

db.sales.find().sort({"date_field": -1}) 

The following examples show how to use each method with a collection sales with the following documents:

db.sales.insertOne({day: new Date("2020-01-20"), amount: 40})
db.sales.insertOne({day: new Date("2020-01-21"), amount: 32})
db.sales.insertOne({day: new Date("2020-01-22"), amount: 19})
db.sales.insertOne({day: new Date("2020-01-23"), amount: 29})
db.sales.insertOne({day: new Date("2020-01-24"), amount: 35})

Example 1: Sort by Date Ascending

We can use the following code to sort all of the documents by date in ascending order:

db.sales.find().sort({"day": 1}) 

This query returns the following results:

{ _id: ObjectId("6189401696cd2ba58ce928fa"),
  day: 2020-01-20T00:00:00.000Z,
  amount: 40 }

{ _id: ObjectId("6189401696cd2ba58ce928fb"),
  day: 2020-01-21T00:00:00.000Z,
  amount: 32 }

{ _id: ObjectId("6189401696cd2ba58ce928fc"),
  day: 2020-01-22T00:00:00.000Z,
  amount: 19 }

{ _id: ObjectId("6189401696cd2ba58ce928fd"),
  day: 2020-01-23T00:00:00.000Z,
  amount: 29 }

{ _id: ObjectId("6189401696cd2ba58ce928fe"),
  day: 2020-01-24T00:00:00.000Z,
  amount: 35 }

Notice that the document with the oldest date (2020-01-20) appears first while the document with the most recent date (2020-01-24) appears last.

Example 2: Sort by Date Descending

We can use the following code to sort all of the documents by date in descending order:

db.sales.find().sort({"day": -1}) 

This query returns the following results:

{ _id: ObjectId("6189401696cd2ba58ce928fe"),
  day: 2020-01-24T00:00:00.000Z,
  amount: 35 }

{ _id: ObjectId("6189401696cd2ba58ce928fd"),
  day: 2020-01-23T00:00:00.000Z,
  amount: 29 }

{ _id: ObjectId("6189401696cd2ba58ce928fc"),
  day: 2020-01-22T00:00:00.000Z,
  amount: 19 }

{ _id: ObjectId("6189401696cd2ba58ce928fb"),
  day: 2020-01-21T00:00:00.000Z,
  amount: 32 }

{ _id: ObjectId("6189401696cd2ba58ce928fa"),
  day: 2020-01-20T00:00:00.000Z,
  amount: 40 }

Notice that the document with the most recent date (2020-01-24) appears first while the document with the oldest date (2020-01-20) appears last.

Note: You can find the complete documentation for the sort function here.

Additional Resources

The following tutorials explain how to perform other common operations in MongoDB:

MongoDB: How to Add a New Field
MongoDB: How to Remove a Field
MongoDB: How to Group By and Count
MongoDB: How to Group By Multiple Fields

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories