DEV Community

Donald Feury
Donald Feury

Posted on • Originally published at donaldfeury.xyz on

3 2

How to sort the results of a MongoDB query

For a full overview of MongoDB and all my posts on it, check out my overview.

After using the find method to get retrieve some documents from MongoDB and further trimming down that result using filters, there may still be a lot of data. It may be easier to work with the data if is it sorted using the sort method.

Much like how a user can modify the result of a query by chaining something like the limit method, you can use chain the sort method to the result of another method that returns a result, such as find or limit, to get that data into a specific order.

With the following data set in a collection called users:

{
    "name": "John Doe",
    "email": "test@test.com",
    "admin": false,
    "dateJoined": ISODate("2021-02-01")
},
{
    "name": "Jane Doe",
    "email": "test2@test2.com",
    "admin": false,
    "dateJoined": ISODate("2021-03-01")
},
{
    "name": "Bob Doe",
    "email": "bob@bob.com",
    "admin": true,
    "dateJoined": ISODate("2021-01-01")
},
{
    "name": "Your Mom",
    "email": "koolkid@someplace.com",
    "admin": false,
    "dateJoined": ISODate("2020-12-01")
}

Enter fullscreen mode Exit fullscreen mode

To pull all the documents out sorted by the date they joined in ascending order, we would do the following:

db.users.find().sort({dateJoined: 1})

Enter fullscreen mode Exit fullscreen mode

Notice that sort takes an argument of an object describing how to sort the data. Each field in the object can have a value of 1 (ascending) or -1 (descending)

This will get us the following:

{
    "name": "Your Mom",
    "email": "koolkid@someplace.com",
    "admin": false,
    "dateJoined": ISODate("2020-12-01")
},
{
    "name": "Bob Doe",
    "email": "bob@bob.com",
    "admin": true,
    "dateJoined": ISODate("2021-01-01")
},
{
    "name": "John Doe",
    "email": "test@test.com",
    "admin": false,
    "dateJoined": ISODate("2021-02-01")
},
{
    "name": "Jane Doe",
    "email": "test2@test2.com",
    "admin": false,
    "dateJoined": ISODate("2021-03-01")
}

Enter fullscreen mode Exit fullscreen mode

Sorting on multiple fields can be done as well

db.users.find().sort({admin: 1, name -1})

Enter fullscreen mode Exit fullscreen mode

This will return the documents first sorted in ascending order by admin, then sorted in descending order by name

{
    "name": "Jane Doe",
    "email": "test2@test2.com",
    "admin": false,
    "dateJoined": ISODate("2021-03-01")
},
{
    "name": "John Doe",
    "email": "test@test.com",
    "admin": false,
    "dateJoined": ISODate("2021-02-01")
},
{
    "name": "Your Mom",
    "email": "koolkid@someplace.com",
    "admin": false,
    "dateJoined": ISODate("2020-12-01")
},
{
    "name": "Bob Doe",
    "email": "bob@bob.com",
    "admin": true,
    "dateJoined": ISODate("2021-01-01")
}

Enter fullscreen mode Exit fullscreen mode

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay