I am trying to write a Mongodb query to sort answers based on the length of upvotes array. The answers having more upvotes should come first. Can anyone please help me with the query?
Here is my collection object:
{
"_id": {
"$oid": "607fcba8a6b6db2830060a2b"
},
"userID": {
"$oid": "60782e1d4709d515c0684c45"
},
"title": "que3",
"description": "text message",
"crowdID": "6042142f5a6e0d1d7e694534",
"userType": "Professional",
"answers": [
{
"_id": {
"$oid": "607fcbb5a6b6db2830060a2d"
},
"userID": {
"$oid": "60782e004709d515c0684c44"
},
"body": "que6",
"comments": [],
"date": {
"$date": "2021-04-21T06:52:37.906Z"
},
"upvotes": []
},
{
"_id": {
"$oid": "607fcbb6a6b6db2830060a2f"
},
"userID": {
"$oid": "60782e004709d515c0684c44"
},
"body": "que6",
"comments": [],
"date": {
"$date": "2021-04-21T06:52:38.706Z"
},
"upvotes": [
{
"_id": {
"$oid": "607fcbefa6b6db2830060a31"
},
"userID": {
"$oid": "607933e4d281292298c99025"
},
"date": {
"$date": "2021-04-21T06:53:35.115Z"
}
},
{
"_id": {
"$oid": "607fcc01a6b6db2830060a33"
},
"userID": {
"$oid": "60782e1d4709d515c0684c45"
},
"date": {
"$date": "2021-04-21T06:53:53.641Z"
}
}
]
},
{
"_id": {
"$oid": "607fcc14a6b6db2830060a35"
},
"userID": {
"$oid": "60782e004709d515c0684c44"
},
"body": "que6",
"comments": [],
"date": {
"$date": "2021-04-21T06:54:12.492Z"
},
"upvotes": []
},
{
"_id": {
"$oid": "607fcc15a6b6db2830060a37"
},
"userID": {
"$oid": "60782e004709d515c0684c44"
},
"body": "que6",
"comments": [],
"date": {
"$date": "2021-04-21T06:54:13.149Z"
},
"upvotes": []
}
],
"date": {
"$date": "2021-04-21T06:52:24.964Z"
},
"__v": 4