I am using a query with aggregation pipeline and want to sort the output based on a string row.
The values look like: “name-1”, “name-2”, …
If I use “$sort” I get the error message: Error running query: string indices must be integers
Is there a way to sort strings?
This site is in read only mode. Please continue to browse, but replying, likes,
and other actions are disabled for now.
What’s the full query you’re using?
{
"collection": "virtualdevices",
"aggregate": [
{
"$match": {
"$and": [
{
"name": {
"$regex": "name-",
"$options": "ig"
}
}
]
}
},
{
"$group": {
"_id": {
"id": "$_id",
"vd": "$name",
"serialNumber": "$serialNumber"
}
}
},
{
"$project": {
"_id": 0,
"value": "$_id.id",
"name": "$_id.vd",
"serialNumber": "$_id.serialNumber"
}
}
]
}
I tried to use:
{ "$sort": { "_id.name": 1 } },
Try changing it into:
{
"$sort": [{
"_id.name": 1
}]
}
"$sort": [{
"name": "name",
"direction": 1
}]
This worked for me!
Thank you!
Why is it like this, though?
No good reason. It just how it was implemented.