If the query bar displays the Sort option, you can specify the sort order of the returned documents.
Set the Sort Order
To set the sort order:
In Atlas, go to the Data Explorer page for your project.
If it's not already displayed, select the organization that contains your project from the Organizations menu in the navigation bar.
If it's not already displayed, select your project from the Projects menu in the navigation bar.
In the sidebar, click Data Explorer under the Database heading.
The Data Explorer displays.
Note
You can also go to the Clusters page, and click Data Explorer under the Shortcuts heading.
Set the sort order.
Select the collection.
In the Query Bar, click Options.
Enter the
sortdocument into the Sort field.To specify ascending order for a field, set the field to
1in the sort document.To specify descending order for a field, set the field and
-1in the sort documents.
Example
The following
sortdocument sorts results first byyearin descending order, and within each year, sort bynamein ascending order.{ year: -1, name: 1 } As you type, the Find button is disabled and the Sort label turns red until a valid query is entered.
Click Find to run the query and view the updated results.
Clear the Query
To clear the query bar and the results of the query, click Reset.
How Does the Atlas Query Compare to MongoDB and SQL Queries?
$sort corresponds to the ORDER BY ... clause
in a SQL SELECT statement.
Example
You have 3,235 articles. You would like to see a list of articles sorted alphabetically by headline.
- SQL
SELECT * FROM article ORDER BY headline ASC; - MongoDB Aggregation
db.article.aggregate( { $sort : { headline : 1 } } ); - Atlas Sort Option
$sort : { headline : 1 }
Learn More
See the sort entry in the
MongoDB Manual.