Suppose I have these documents:
[
{
'_id': 1,
'roles': [
{
'k': 'free',
'v': 1
},
{
'k': 'pro',
'v': 5
},
{
'k': 'free',
'v': 2
}
]
},
{
'_id': 2,
'roles': [
{
'k': 'pro',
'v': 1
},
{
'k': 'free',
'v': 3
},
{
'k': 'free',
'v': 2
}
]
}
]
So for every _id, we have a array of documents called roles.
I need to sort inside the array roles, using the v field.
Expected output:
[
{
'_id': 1,
'roles': [
{
'k': 'free',
'v': 1
},
{
'k': 'free',
'v': 2
}
{
'k': 'pro',
'v': 5
}
]
},
{
'_id': 2,
'roles': [
{
'k': 'pro',
'v': 1
},
{
'k': 'free',
'v': 2
}
{
'k': 'free',
'v': 3
}
]
}
]
So I tried to use $sort:
{
'$sort': {
'roles.v': 1
}
}
But it does not sort inside the array.