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?

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?

Can you try this syntax:

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

Error running query: string indices must be integers

Did you ever work this out @Digitz?

It works when I try it like this (mind the square brackets):

{
   "$sort": [{
       "name": "likes",
       "direction": 1
   }]
}
3 Likes

I can confirm what @daan wrote above. $sort has been performing properly using his syntax.

Try this, If MongoDB with limit

{
“collection” : “collection_name”,
“aggregate” : [
{
“$sort”: [{
“name”: “column_name”,
“direction”: -1
}]
},
{"$limit" : 500}
]
}

1 Like