Below is sample doc of collection.
{
name:"sam",
age:20,
hobbies: [ {id:1, value: "football"}, {id:2 value:"chess"} ]
},
{
"name": "Bob",
"age": 30,
"hobbies": [
{ "id": 1, "value": "painting" },
{ "id": 2, "value": "gardening" }
]
},
{
"name": "Charlie",
"age": 22,
"hobbies": [
{ "id": 1, "value": "cycling" },
{ "id": 2, "value": "playing the guitar" },
{ "id": 3, "value": "photography" }
]
}
I have tried using the below query, it only sorts the array of hobbies not the documents of the collection.
{$sort : {"hobbies.value": 1}}
I want to sort documents of the collection based on hobbies.value and also sort objects inside the array.
The expected result will be something like below for sort: 1.
```{
name:"sam",
age:20,
hobbies: [ {id:2 value:"chess"}, {id:1, value: "football"} ]
},
{
"name": "Charlie",
"age": 22,
"hobbies": [
{ "id": 1, "value": "cycling" },
{ "id": 2, "value": "playing the guitar" },
{ "id": 3, "value": "photography" }
]
},
{
"name": "Bob",
"age": 30,
"hobbies": [
{ "id": 1, "value": "painting" },
{ "id": 2, "value": "gardening" }
]
}
also, the hobbies could be null and hobbies values could be numbers and special characters.