MongoDB sort

MongoDB sort | Complete tutorial with examples in [2022]

MongoDB has a vibrant command-line tool to interact with mongo and perform all the operations. One such command is the MongoDB sort command. In this blog, we will deep dive into the sort command and understand it with various examples.

MongoDB sort() method

The sort() method in mongodb is used to sort the data in a particular order based on the user’s requirement. The general syntax for the sort() command follows the below pattern

db.<collection_name>.find({<condition>}).sort({field_name: 1 or -1})

where

  • <collection_name> = name of the mongo collection
  • find({<condition>}) = command is used to filter out the data before sorting
  • 1 means sort in asc order -1 means sort in desc order.

To get started with the mongodb sort() command, let’s first insert the below documents into the mongodb weather collection.

db.weather.insertMany([{
   "metadata": [{"sensorId": 5578}, {"type": "temperature"}],
   "date": ISODate("2021-05-18T00:00:00.000Z"),
   "temp": 12
}, {
   "metadata": [{"sensorId": 5578}, {"type": "temperature"}],
   "date": ISODate("2021-05-18T04:00:00.000Z"),
   "temp": 11
}, {
   "metadata": [{"sensorId": 5578}, {"type": "temperature"}],
   "date": ISODate("2021-05-18T08:00:00.000Z"),
   "temp": 11
}, {
   "metadata": [{"sensorId": 5578}, {"type": "temperature"}],
   "date": ISODate("2021-05-18T12:00:00.000Z"),
   "temp": 12
}, {
   "metadata": [{"sensorId": 5578}, {"type": "temperature"}],
   "date": ISODate("2021-05-18T16:00:00.000Z"),
   "temp": 16
}, {
   "metadata": [{"sensorId": 5578}, {"type": "temperature"}],
   "date": ISODate("2021-05-18T20:00:00.000Z"),
   "temp": 15
}, {
   "metadata": [{"sensorId": 5578}, {"type": "temperature"}],
   "date": ISODate("2021-05-19T00:00:00.000Z"),
   "temp": 13
}, {
   "metadata": [{"sensorId": 5578}, {"type": "temperature"}],
   "date": ISODate("2021-05-19T04:00:00.000Z"),
   "temp": 12
}, {
   "metadata": [{"sensorId": 5578}, {"type": "temperature"}],
   "date": ISODate("2021-05-19T08:00:00.000Z"),
   "temp": 11
}, {
   "metadata": [{"sensorId": 5578}, {"type": "temperature"}],
   "date": ISODate("2021-05-19T12:00:00.000Z"),
   "temp": 12
}, {
   "metadata": [{"sensorId": 5578}, {"type": "temperature"}],
   "date": ISODate("2021-05-19T16:00:00.000Z"),
   "temp": 17
}, {
   "metadata": [{"sensorId": 5578}, {"type": "temperature"}],
   "date": ISODate("2021-05-19T20:00:00.000Z"),
   "temp": 12
}])

verify if the documents get properly inserted by typing the below command

db.weather.find()
{ "_id" : ObjectId("61370840925c215ff58518ee"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-18T00:00:00Z"), "temp" : 12 }
{ "_id" : ObjectId("61370840925c215ff58518ef"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-18T04:00:00Z"), "temp" : 11 }
{ "_id" : ObjectId("61370840925c215ff58518f0"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-18T08:00:00Z"), "temp" : 11 }
{ "_id" : ObjectId("61370840925c215ff58518f1"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-18T12:00:00Z"), "temp" : 12 }
{ "_id" : ObjectId("61370840925c215ff58518f2"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-18T16:00:00Z"), "temp" : 16 }
{ "_id" : ObjectId("61370840925c215ff58518f3"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-18T20:00:00Z"), "temp" : 15 }
{ "_id" : ObjectId("61370840925c215ff58518f4"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T00:00:00Z"), "temp" : 13 }
{ "_id" : ObjectId("61370840925c215ff58518f5"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T04:00:00Z"), "temp" : 12 }
{ "_id" : ObjectId("61370840925c215ff58518f6"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T08:00:00Z"), "temp" : 11 }
{ "_id" : ObjectId("61370840925c215ff58518f7"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T12:00:00Z"), "temp" : 12 }
{ "_id" : ObjectId("61370840925c215ff58518f8"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T16:00:00Z"), "temp" : 17 }
{ "_id" : ObjectId("61370840925c215ff58518f9"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T20:00:00Z"), "temp" : 12 }

In the subsequent sessions, we will understand how to sort the data based on specific fields.

mongodb sort examples

Mongodb sort by _id

The field name _id is reserved for the primary key, and its value must be unique in the collection. If you insert a record in mongodb without mentioning a “_id” field,mongodb will add this field in all the documents.

To sort the weather data with “_id” field, type the below command

db.weather.find().sort({"_id":1})
{ "_id" : ObjectId("61370840925c215ff58518ee"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-18T00:00:00Z"), "temp" : 12 }
{ "_id" : ObjectId("61370840925c215ff58518ef"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-18T04:00:00Z"), "temp" : 11 }
{ "_id" : ObjectId("61370840925c215ff58518f0"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-18T08:00:00Z"), "temp" : 11 }
{ "_id" : ObjectId("61370840925c215ff58518f1"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-18T12:00:00Z"), "temp" : 12 }
{ "_id" : ObjectId("61370840925c215ff58518f2"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-18T16:00:00Z"), "temp" : 16 }
{ "_id" : ObjectId("61370840925c215ff58518f3"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-18T20:00:00Z"), "temp" : 15 }
{ "_id" : ObjectId("61370840925c215ff58518f4"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T00:00:00Z"), "temp" : 13 }
{ "_id" : ObjectId("61370840925c215ff58518f5"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T04:00:00Z"), "temp" : 12 }
{ "_id" : ObjectId("61370840925c215ff58518f6"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T08:00:00Z"), "temp" : 11 }
{ "_id" : ObjectId("61370840925c215ff58518f7"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T12:00:00Z"), "temp" : 12 }
{ "_id" : ObjectId("61370840925c215ff58518f8"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T16:00:00Z"), "temp" : 17 }
{ "_id" : ObjectId("61370840925c215ff58518f9"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T20:00:00Z"), "temp" : 12 }
  • here we are specifying find() without any filter condition,it menas we need all the data to be sorted.
  • sort({“_id”:1}) means we need to sort the data by _id by asc order.

Mongodb find and sort

Sometimes users do not want to sort the entire data; instead, they need to find some data before filtering it out. Use the below code snippet to achieve this

use case: Filter out the document where temp=12 and then sort the data based on “_id” in asc order.

db.weather.find({"temp": 12}).sort({"_id":1})
{ "_id" : ObjectId("61370840925c215ff58518ee"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-18T00:00:00Z"), "temp" : 12 }
{ "_id" : ObjectId("61370840925c215ff58518f1"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-18T12:00:00Z"), "temp" : 12 }
{ "_id" : ObjectId("61370840925c215ff58518f5"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T04:00:00Z"), "temp" : 12 }
{ "_id" : ObjectId("61370840925c215ff58518f7"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T12:00:00Z"), "temp" : 12 }
{ "_id" : ObjectId("61370840925c215ff58518f9"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T20:00:00Z"), "temp" : 12 }
  • find({“temp”: 12}) = This code is used to filter out the data where temp=12.
  • sort({“_id”:1}) means we need to sort the data by _id by asc order.

Mongodb sort and limit

Using the mongodb limit() command, we can limit the data while filtering out the data. Type the below command to filter out the data before sorting

use case: Filter out the 3 data where temp=12 and then sort the data based on “_id” in desc order.

db.weather.find({"temp": 12}).sort({"_id": -1}).limit(3)
{ "_id" : ObjectId("61370840925c215ff58518f9"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T20:00:00Z"), "temp" : 12 }
{ "_id" : ObjectId("61370840925c215ff58518f7"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T12:00:00Z"), "temp" : 12 }
{ "_id" : ObjectId("61370840925c215ff58518f5"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T04:00:00Z"), "temp" : 12 }
  • find({“temp”: 12}) = This method is used to filter out the data where temp=12.
  • sort({“_id”: -1}) = means we need to sort the data by _id by desc order.
  • limit(3) = is used to limit the data by 3 document.

MongoDB sort using multiple fields

To sort the document with multiple fields, we must specify multiple fields in the sort() method. Let’s try to implement the below use case.

Use case:sort the data by temp and date in asc order.

db.weather.find().sort({"temp" : 1, "date": 1})
{ "_id" : ObjectId("61370840925c215ff58518ef"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-18T04:00:00Z"), "temp" : 11 }
{ "_id" : ObjectId("61370840925c215ff58518f0"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-18T08:00:00Z"), "temp" : 11 }
{ "_id" : ObjectId("61370840925c215ff58518f6"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T08:00:00Z"), "temp" : 11 }
{ "_id" : ObjectId("61370840925c215ff58518ee"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-18T00:00:00Z"), "temp" : 12 }
{ "_id" : ObjectId("61370840925c215ff58518f1"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-18T12:00:00Z"), "temp" : 12 }
{ "_id" : ObjectId("61370840925c215ff58518f5"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T04:00:00Z"), "temp" : 12 }
{ "_id" : ObjectId("61370840925c215ff58518f7"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T12:00:00Z"), "temp" : 12 }
{ "_id" : ObjectId("61370840925c215ff58518f9"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T20:00:00Z"), "temp" : 12 }
{ "_id" : ObjectId("61370840925c215ff58518f4"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T00:00:00Z"), "temp" : 13 }
{ "_id" : ObjectId("61370840925c215ff58518f3"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-18T20:00:00Z"), "temp" : 15 }
{ "_id" : ObjectId("61370840925c215ff58518f2"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-18T16:00:00Z"), "temp" : 16 }
{ "_id" : ObjectId("61370840925c215ff58518f8"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T16:00:00Z"), "temp" : 17 }
  • find() = This method is used to fetch all the data.
  • sort({“temp” : 1, “date”: 1}) = Here we have passed temp and date both the field in sort command which will basically sort the date first by temp in asc order then by date in asc order.

MongoDB aggregate and sort

This session will perform advance aggregation in mongodb via the aggregate() method.

use case: aggregate the data based on temp and sort the id by desc order

db.weather.aggregate({ $group: { "_id":'$temp', "temp": { $sum:1}}}, {$sort: {"_id": -1}})
{ "_id" : 17, "temp" : 1 }
{ "_id" : 16, "temp" : 1 }
{ "_id" : 15, "temp" : 1 }
{ "_id" : 13, "temp" : 1 }
{ "_id" : 12, "temp" : 5 }
{ "_id" : 11, "temp" : 3 }
  • aggregate({ $group: { “_id”:’$temp’, “temp”: { $sum:1}}} = here by using aggregate() method i am counting the no of unique temp and saving the _id as the temp value.
  • {$sort: {“_id”: -1} = sort() method is used to sort the data by _id(which is basically the temp) in desc order

Mongodb sort by date

Mongodb sort by date

In this session, we will understand how to sort the mongo data by date while querying.

Mongodb sort by date asc

In almost all the mongo collections, users add documents with date fields to filter out the data by date whenever required.

Use case: Sort the data by date in asc order where “temp”: 12

db.weather.find({ "temp": 12 }).sort({"date": 1})
{ "_id" : ObjectId("61370840925c215ff58518ee"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-18T00:00:00Z"), "temp" : 12 }
{ "_id" : ObjectId("61370840925c215ff58518f1"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-18T12:00:00Z"), "temp" : 12 }
{ "_id" : ObjectId("61370840925c215ff58518f5"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T04:00:00Z"), "temp" : 12 }
{ "_id" : ObjectId("61370840925c215ff58518f7"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T12:00:00Z"), "temp" : 12 }
{ "_id" : ObjectId("61370840925c215ff58518f9"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T20:00:00Z"), "temp" : 12 }
  • find({“temp”: 12}) = is used to filter out the data where temp=12
  • sort({“date”: 1}) = means we need to sort the data by date by asc order.

Mongodb sort by date desc

Type the below command to sort the data by date in desc order.

Use case: Sort the data by date in desc order

db.weather.find().sort({"date": -1})
{ "_id" : ObjectId("61370840925c215ff58518f9"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T20:00:00Z"), "temp" : 12 }
{ "_id" : ObjectId("61370840925c215ff58518f8"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T16:00:00Z"), "temp" : 17 }
{ "_id" : ObjectId("61370840925c215ff58518f7"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T12:00:00Z"), "temp" : 12 }
{ "_id" : ObjectId("61370840925c215ff58518f6"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T08:00:00Z"), "temp" : 11 }
{ "_id" : ObjectId("61370840925c215ff58518f5"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T04:00:00Z"), "temp" : 12 }
{ "_id" : ObjectId("61370840925c215ff58518f4"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-19T00:00:00Z"), "temp" : 13 }
{ "_id" : ObjectId("61370840925c215ff58518f3"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-18T20:00:00Z"), "temp" : 15 }
{ "_id" : ObjectId("61370840925c215ff58518f2"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-18T16:00:00Z"), "temp" : 16 }
{ "_id" : ObjectId("61370840925c215ff58518f1"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-18T12:00:00Z"), "temp" : 12 }
{ "_id" : ObjectId("61370840925c215ff58518f0"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-18T08:00:00Z"), "temp" : 11 }
{ "_id" : ObjectId("61370840925c215ff58518ef"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-18T04:00:00Z"), "temp" : 11 }
{ "_id" : ObjectId("61370840925c215ff58518ee"), "metadata" : [ { "sensorId" : 5578 }, { "type" : "temperature" } ], "date" : ISODate("2021-05-18T00:00:00Z"), "temp" : 12 }
  • find() = This code is used to find all the data
  • sort({“date”: -1}) = means we need to sort the data by date by asc order.

Conclusion

I hope you like this tutorial. This tutorial has explored various use cases where the sort() command can be used. Please do let me know if you face any difficulty in following along. Happy learning 🙂

More to read?

MongoDB with python | pymongo

Install MongoDB using docker

MongoDB commands you should be aware of

MongoDB vs Mysql

Mongodb vs PostgreSQL

Leave a Comment

Your email address will not be published.

Scroll to Top