sort
$sort
Sorts documents in ascending or descending order based on one or more fields for the next stage in the aggregation pipeline.
Syntax
{ "$sort": { "<field1>": "<sort order>", "<field2>": "<sort order>"} }
Consider the following points for your sort criteria:
- Enter
1to sort in ascending order for that field's values - Enter
-1to sort in descending order for that field's values - If you pass multiple sort criteria, the first criterion is the primary sort, and the second criterion sorts fields that are equal by the first criterion.
Sample Request
[
{
"$match": {
"temp": {
"$exists": true
}
}
},
{
"$sort":{
"temp":1
}
},
{
"$limit":3
}
]
Sample Response
{
"_list": [
{
"temp": 10,
"_tsMetadata": {
"_sourceId": "4b78c28b-7af6-406f-9265-b120c7141b13"
},
"_id": "645bcb5cf8b8a87c0f075cf7",
"_ts": "Wed May 10 22:20:35 IST 2023"
},
{
"temp": 11,
"_tsMetadata": {
"_sourceId": "0dda42b7-831b-47d2-aa83-c65edd3c5113"
},
"_id": "645bcb5cf8b8a87c0f075cf8",
"_ts": "Wed May 10 22:20:36 IST 2023"
}
{
"temp": 12,
"_tsMetadata": {
"_sourceId": "4b78c28b-7af6-406f-9265-b120c7141b13"
},
"_id": "645bcb5cf8b8a87c0f075d00",
"_ts": "Wed May 10 22:20:36 IST 2023"
}
]
}