Digitz
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?
arikfr
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?
levko
3
Can you try this syntax:
{
"$sort": {
"name": "likes",
"direction": 1
}
}
Digitz
4
Error running query: string indices must be integers
jesse
5
Did you ever work this out @Digitz?
daan
6
It works when I try it like this (mind the square brackets):
{
"$sort": [{
"name": "likes",
"direction": 1
}]
}
3 Likes
jesse
7
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