I have a document which looks something like:
{
"_id" : ObjectId("4e84f78b26d2046d5d00b5b2"),
"parent_id" : 0,
"ratings" : [
"20716",
"78167"
],
"text" : "test"
}
Is it possible to sort by the count of “ratings”? Doing something like:
db.comments.find().sort({rating.count(): -1})
throws an error:
SyntaxError: missing : after property id (shell):0
That’s not directly possible with mongodb [1]
One solution, if you frequently use this query, is to increment and decrement a field ‘count’ each time you modify the ratings array and sort on that field.
You document will look like :
and you query with