$sort is not working correctly on mongo db aggregate stages


#1

Hello,

I has been trying to execute below query but failed
{
“collection”: “projects”,
“aggregate”: [
{
“$project”: {
“title”: 1,
“likes”: {
“$size”: “$likes”
}
}
},
{
“$sort”: { “likes”: 1 }
}
]
}

This works on MongoDB Compass.

Can you fix it?


#2

It could help if you can share more information on how this failed? Did it return some error message? Did it return the wrong results?


#3

Can you try this syntax:

{
  "$sort": { 
    "name": "likes",
    "direction": 1
  }
}