mongoose - sort by array length
You can't do that directly. To be able to sort on array length, you have to maintain it in a separate field (votes_count
, or whatever) and update it when you push/pull elements to/from votes
.
Then you sort on that votes_count
field. If you also index it, queries will be faster.