I have a document having array structure in MongoDB. How can I sort the array element in ascending order?
The below is the Input:
{
"id": 1,
"name": "sanjay",
"address": "Odisha",
"shoes": [
{
"brand": "puma",
"size": "10"
},
{
"brand": "bata",
"size": "11"
},
{
"brand": "rebook",
"size": "9"
},
{
"brand": "adidas",
"size": "7"
},
{
"brand": "bata",
"size": "8"
}
]
}
Can anyone help me to get a query to sort my all array element in ascending order of the key "size"?
The below is the required output.
{
"id": 1,
"name": "sanjay",
"address": "Odisha",
"shoes": [
{
"brand": "adidas",
"size": "7"
},
{
"brand": "bata",
"size": "8"
},
{
"brand": "rebook",
"size": "9"
},
{
"brand": "puma",
"size": "10"
},
{
"brand": "bata",
"size": "11"
}
]
}
Could you please help me to find out the exact query?
Regards, Sanjay