xxxxxxxxxx
>>> from bson.son import SON
>>> pipeline = [
"$unwind": "$tags"}, {
"$group": {"_id": "$tags", "count": {"$sum": 1}}}, {
"$sort": SON([("count", -1), ("_id", -1)])} {
]
>>> list(db.things.aggregate(pipeline))
[{u'count': 3, u'_id': u'cat'}, {u'count': 2, u'_id': u'dog'}, {u'count': 1, u'_id': u'mouse'}]