Sort by length of array

191 views
Skip to first unread message

Nico Schlömer

unread,
Feb 24, 2016, 12:31:19 PM2/24/16
to mongoose-users
I have a schema that goes something like
```
var PostSchema = new Schema({
  color: {type: String},
  votes: [{ type: Schema.ObjectId, ref: 'User' }]
})
```
I would now like to sort by "most votes", i.e., sort by the length of an array. The question came up several years ago [1] and at the time, the answer was that

> 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).

Is this still up-to-date?

Cheers,
Reply all
Reply to author
Forward
0 new messages