JIYIK CN >

Current Location:Home > Learning > DATABASE > MongoDB >

Sorting by timestamp in MongoDB

Author:JIYIK Last Updated:2025/04/27 Views:

This article will introduce various methods of sorting timestamps in MongoDB.


Sorting by timestamp in MongoDB

sort()The method will sort documents in MongoDB. The method accepts a document containing a list of fields and the order in which the fields should be sorted.

1 and -1 specify the sort order. For ascending order, use 1; for descending order, use -1.

grammar:

db.collection_name.find().sort({ field_name : 1 | -1 })

In this case, the collection on which you want to perform the sort is specified by collection_name. The field that needs to be sorted in ASC or DESC order is specified by field_name.

If you do not specify a preferred sort order, the sort() method displays the pages in ascending order. An unstable sort produces different results each time it is applied to the same data set.

Using the index, MongoDB can locate the result of the sort operation. If the index scan cannot determine the sort order, MongoDB will perform a top-k sort algorithm.

Consider the following example to help you better understand the previous idea.

> db.employees.find().sort({ "joining_date" : -1 })

In the above example, we sort the employees based on their Join Date in the Employees collection. The Join Date column tracks the ISO formatted Join Date of the employee.

If you wish to sort based on the created_at field, you can also use _idsince it has a timestamp. This will lead to the same result.

> db.employees.find().sort({ "_id" : -1 })

or

> db.employees.find().sort({ "create_at" : -1 })

Run the above line of code in a MongoShell that is compatible with MongoDB. It will display the following result:

{
  "_id" : ObjectId("54f612b6029b47919a90cesd"),
  "email" : "johndoe@exampledomain.com",
  "name" : "John Doe",
  "create_at" : "ISODate("2020-07-04T00:00:00Z")",
  "joining_date" : "ISODate("2020-07-04T00:00:00Z")"
}
{
  "_id" : ObjectId("54f612b6029b47919a97cesd"),
  "email" : "smithwarn@exampledomain.com",
  "name" : "Smith Warn",
  "create_at" : "ISODate("2020-04-28T00:00:00Z")",
  "joining_date" : "ISODate("2020-04-28T00:00:00Z")"
}
{
  "_id" : ObjectId("54f612b6029b47919a91cesd"),
  "email" : "jessicawill@exampledomain.com",
  "name" : "Jessica Will",
  "create_at" : "ISODate("2019-12-14T00:00:00Z")",
  "joining_date" : "ISODate("2019-12-14T00:00:00Z")"
}

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

$unset operator in MongoDB

Publish Date:2025/04/27 Views:77 Category:MongoDB

This article will discuss how the $unset operator works in MongoDB. Additionally, we will demonstrate the use of this operator to remove a field from all documents in a MongoDB collection. $unset operator in MongoDB $unset is an operator us

Compass Filters in MongoDB

Publish Date:2025/04/27 Views:132 Category:MongoDB

This short article will cover the various ways to use Compass filters in MongoDB . Compass Filters in MongoDB MongoDB has a GUI called Compass . It is also known as MongoDB GUI. Users can use MongoDB to inspect the contents of their stored

$ne operator in MongoDB

Publish Date:2025/04/11 Views:84 Category:MongoDB

This article will discuss how the $ne operator works in MongoDB. In addition, we will list its differences from the $not operator. $ne operator in MongoDB $ne is an operator in MongoDB that stands for not equal to. This will compare the val

MongoDB $Set Operator

Publish Date:2025/04/11 Views:159 Category:MongoDB

With the help of this article, you will learn how to use $set the operator to partially update objects in MongoDB so that the new object overlaps/merges with the existing object. The $set operator replaces the value of a field with a given

Difference between $push and $addToSet in MongoDB

Publish Date:2025/04/11 Views:63 Category:MongoDB

This article explains the operators in MongoDB. What is the purpose of $push and $addToSet operators. Furthermore, the difference between these two operators is given in the code snippet. This article discusses the following topics. Operato

Sort a collection by date in MongoDB

Publish Date:2025/04/11 Views:64 Category:MongoDB

In this MongoDB tutorial, the problem of sorting a collection in MongoDB is discussed. The different ways to sort a collection in the database are briefly explained. Using sort() function in MongoDB This problem is solved using the MongoDB

Counting records in MongoDB

Publish Date:2025/04/11 Views:146 Category:MongoDB

This article discusses operators in MongoDB, aggregation operators, and different ways to calculate the total number of records. Operations in MongoDB CRUD operations are a user interface concept that allows users to browse, search, and cha

Pretty printing in MongoDB

Publish Date:2025/04/11 Views:150 Category:MongoDB

This article will discuss how to use pretty printing in MongoDB to display formatted results. Pretty printing in MongoDB A cursor is an object that allows programmers in the Mongo world to iterate over documents in a Mongo collection. Altho

MongoDB Adding Elements to an Array

Publish Date:2025/04/11 Views:136 Category:MongoDB

This article will cover the various ways to add to an array in MongoDB. Adding to an array in MongoDB Use the $push operator to add values ​​to an array The $push operator is one of the various array update operators provided by MongoDB

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial