Database

[{"collection":"collection","count":10,"content":{"title":{"type":"faker","method":"BeerName"},"description":{"type":"faker","method":"HackerPhrase"},"likes":{"type":"array","minLength":3,"maxLength":20,"arrayContent":{"type":"faker","method":"Username"}},"hates":{"type":"array","minLength":3,"maxLength":20,"arrayContent":{"type":"faker","method":"Username"}}}}]

Query

db.collection.aggregate([{/** Count likes and hates*/"$set":{"likeCount":{"$size":"$likes"},"hateCount":{"$size":"$hates"}}},{/** Most likes first, split ties with* least hates first*/"$sort":{"likeCount":-1,"hateCount":1}},/** {* "$project": {* "likeCount": 0,* "hateCount": 0* }* }*/])

Result